Zend Framework: How to concatenate two columns and still use fetchPairs()?

后端 未结 2 995
我寻月下人不归
我寻月下人不归 2021-01-21 04:43

I have a form with a select element that I need to populate with values from a database. Specifically, the name and id of the current users. The fetchPairs() functi

相关标签:
2条回答
  • 2021-01-21 05:18
    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);
    }
    
    0 讨论(0)
  • 2021-01-21 05:22

    This is only an idea, and fully untested, but you could try to use a 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;
    

    From the Zend_Db documentation:

    Example 15.55. Examples of specifying columns containing expressions

    An expression with parentheses implicitly becomes a Zend_Db_Expr.

    0 讨论(0)
提交回复
热议问题