CakePHP form select value as table.id, select option text as table.textfield

前端 未结 1 1884
温柔的废话
温柔的废话 2021-01-20 08:32

I\'m new to Cake. I\'ve gotten some things set up, relevantly a \"users\" and a \"profiles\" table, controller, model and view. In my profiles/add view, I\'m at

1条回答
  •  感情败类
    2021-01-20 08:59

    successfully set up the select to output the options from the user tables, but it uses the user.id as both the value and the option text.

    find('list') will by default use the primary key as the key, and the model's displayField as the value.

    If a model does not have an explicit displayField but has a field named title or name Cake will automatically pick this field, otherwise it will not guess and simply use the primary-key field - which is what's happening in the question.

    You can however simply specify which fields you want in the results of your find('list') call. Therefore either:

    Set the model displayField

    class User extends AppModel {
    
        public $displayField = 'username';
    
    }
    

    and then use:

    $users = $this->User->find('list');
    

    Explicitly choose the fields

    $users = $this->User->find('list', array(
        'fields' => array('id', 'username')
    ));
    

    In both cases - the result will be e.g.:

    array(
        1 => 'firstuser',
        ...
        76 => 'AD7six'
    )
    

    0 讨论(0)
提交回复
热议问题