Zend Framework: how to combine two columns and use fetchPairs ()?

I have a form with a select element that I need to populate with values ​​from the database. In particular, the name and identifier of current users. Function fetchPairs()works great for this! However, I need to combine the value from the column first_nameand column last_nameand display it as a parameter label. Is there any way to do this and still use fetchPairs()? If not, how can I achieve the same result? Here is an excerpt from the code that is currently working:

<?php // excerpt

class Default_Form_AddUser extends Zend_Form
{
    public function init()
    {
        $this->addElement('select', 'user', array(
            'label'      => 'Select user:',
            'required'   => true,
            'multiOptions' => $this->_getSelectOptions()
        ));
    }

    protected function _getSelectOptions()
    {
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()->from('users', array('id', 'first_name'));
        $roleOptions = $db->fetchPairs($select);
        return $roleOptions;
    }
}
+3
source share
2 answers
protected function _getSelectOptions()
{
    $db     = Zend_Db_Table::getDefaultAdapter();
    $select = $db->select()->from('users', array(
        'id',
        'name' => new Zend_Db_Expr("CONCAT(first_name, ' ', lastname)"),
    ));

    return $db->fetchPairs($select);
}
+11
source

, Zend_Db_Expr:

$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select()->from(
   'users', 
    array('id', "CONCAT_WS(' ', first_name, last_name")
);
$roleOptions = $db->fetchPairs($select);
return $roleOptions;

Zend_Db:

15.55. ,

Zend_Db_Expr.

+3

Source: https://habr.com/ru/post/1723930/


All Articles