cakephp find list

后端 未结 5 666
陌清茗
陌清茗 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:12

    I think this can be done using the virtualFields and displayField properties in your model.

    In your model, define a virtual field for the full name like this:

    public $virtualFields = array(
        'full_name' => 'CONCAT(User.first_name, " ", User.last_name)'
    );
    

    If you now set displayField to full_name you should be able to get a list of your users with the $this->User->find('list') method which you can use without problems with the Form-helper.

    public $displayField = 'full_name';
    

    ... or:

    public $displayField = 'User.full_name';
    

    The id is fetched automatically.

提交回复
热议问题