我正在使用cakephp 3.3 我在控制器中
$employee = TableRegistry::get('employees');
$allNames = $employee->find('list', array('employee_name' => array('employee_name') ) );
$allNames = $allNames->toArray();
$this->set('name', $allNames);
并在我的模板中
<?= $this->Form->input('employee', array('type'=>'select','label' => false,
'options' => $name,'value'=>$name));?>
唯一检索和显示的是我在数据库中的条目数,$ name只包含一个数组(1,2,3,4),它应该是实际的人名。
答案 0 :(得分:3)
您是否阅读过documentation find('list')
如何运作?'
$allNames = $employee->find('list', [
'keyField' => 'employee_name'
'valueField' => 'employee_name'
]);
$this->set('name', $allNames);
<?= $this->Form->input('employee', ['type'=>'select','label' => false,
'options' => $name]);?>
答案 1 :(得分:1)
您可以在列表查询
中按keyField
和valueField
映射ID和值
控制器中的
$employee = TableRegistry::get('employees');
$allNames = $employee->find('list', array('valueField' =>employee_name','keyField'=>'employee_id' )); // correct employee_id with your table id field.
$this->set('name', $allNames);
在您的视图中
<?= $this->Form->input('employee', array('type'=>'select','label' => false,
'options' => $name));?>