我怎样才能将first_name和last_name连接两个不同的数据库字段,以便在find语句中使用'list'分配给cakephp中的下拉列表

时间:2011-08-02 06:42:11

标签: cakephp

$swimmer = $this->Swimmer->find('list', array(
    'conditions' => array('Swimmer.group' => $this->data['Swimmer']['group_id']),
    'order' => array('Swimmer.first_name ASC'),
    'fields' => 'Swimmer.first_name'
));

3 个答案:

答案 0 :(得分:7)

首先,按照Ann Pham的建议,在Swimmer模型中设置virtual field。例如:

var $virtualFields = array(
    'name' => "CONCAT(Swimmer.first_name, ' ', Swimmer.last_name)"
);

然后,获取下拉列表的数据,如下所示:(假设Swimmers控制器)

$this->Swimmer->find('list', array('fields' => array('Swimmer.id', 'Swimmer.name')));

您也可以尝试在SwimmerModel中执行此操作:var $displayName = 'Swimmer.name';(不是100%确定是否可行)。如果它确实有效,你将不需要查找中的'fields'数组。

答案 1 :(得分:0)

答案 2 :(得分:0)

另一种方法是在字段列表中即时连接字段。

例如:

$swimmer = $this->Swimmer->find('list',
array(
    'conditions' => array(
        'Swimmer.group' => $this->data['Swimmer']['group_id']),
    'order' => array(
        'Swimmer.first_name ASC'
    ),
    'fields' => 'CONCAT(Swimmer.first_name, " ", Swimmer.first_name) AS full_name'
));