cakephp find list

后端 未结 5 636
陌清茗
陌清茗 2020-12-31 09:55

Hi I want to be able to generate a list using find so that I can use in select helper. but there is a problem. i want too fetch id,name(first + last). so how can I achieve i

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 10:13

    Another solution is to use Cake's Set::combine to build what you need...

    $users = $this->User->find('all',array('fields' => array('first_name','last_name','id')));
    
    $user_list = Set::combine($users, '{n}.User.id', array('{0} {1}', '{n}.User.first_name', '{n}.User.last_name'));
    

    Result will look something like:

    array(
     [2] => 'First Last',
     [5] => 'Bob Jones'
    )
    

    Here's the documentation link:

    http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::combine

提交回复
热议问题