开发者

how can i concat first_name and last_name two different database fields to assign to drop down list in cakephp using 'list' in find statement

开发者 https://www.devze.com 2023-03-25 11:14 出处:网络
$swimmer = $this->Swimmer->find(\'list\', array( \'condition开发者_如何学Pythons\' => array(\'Swimmer.group\' => $this->data[\'Swimmer\'][\'group_id\']),
$swimmer = $this->Swimmer->find('list', array(
    'condition开发者_如何学Pythons' => array('Swimmer.group' => $this->data['Swimmer']['group_id']),
    'order' => array('Swimmer.first_name ASC'),
    'fields' => 'Swimmer.first_name'
));


First of all, setup a virtual field in your Swimmer model as suggested by Ann Pham. eg:

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

Then, fetch the data for your dropdown list like so: (assuming Swimmers controller)

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

You could also try doing this in your SwimmerModel: var $displayName = 'Swimmer.name'; (not 100% sure if this would work). If it does work, you won't need the 'fields' array in the find.


An another approach is to concat the field on the fly in the fields list.

Ex:

$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'
));


http://book.cakephp.org/view/1588/virtualFields

0

精彩评论

暂无评论...
验证码 换一张
取 消