Zend Framework:如何连接两列并仍然使用 fetchPairs()? [英] Zend Framework: How to concatenate two columns and still use fetchPairs()?
本文介绍了Zend Framework:如何连接两列并仍然使用 fetchPairs()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有 select 元素的表单,我需要用数据库中的值填充该元素.具体来说,当前用户的名称和 ID.fetchPairs()
函数非常适合这个!但是,我需要连接 first_name
列和 last_name
列中的值并将其显示为选项标签.有没有办法做到这一点并且仍然使用 fetchPairs()
?如果没有,我怎样才能达到同样的结果?以下是当前正在运行的代码的摘录:
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()
function works great for this! However, I need to concatenate the value from the first_name
column and the last_name
column and display this as the option label. Is there a way to do it and still use fetchPairs()
? If not, how can I achieve the same result? Here's an excerpt of 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;
}
}
推荐答案
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);
}
这篇关于Zend Framework:如何连接两列并仍然使用 fetchPairs()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文