Select 输入 ID 和名称 cakephp3

Select input with id and name cakephp3

在我的控制器中,制作这个过滤器

 $categories = $this->Posts->Categories->find('list', ['limit' => 200])->select(['id', 'category_name']);

在我看来

echo $this->Form->select('category_id', $categories->toArray());

但是,数组中返回的数据只是代码(在本例中为 id)。如何用id和category_name实现一个select?应该手动做吗?

谢谢...

在这种情况下,您不应该使用 select。使用 displayField 告诉它你想要 category_name,或者在 list 调用的选项中指定 keyFieldvalueField 参数,按照 the manual.

$this->Posts->Categories->displayField('category_name');
$categories = $this->Posts->Categories->find('list', ['limit' => 200]);

$categories = $this->Posts->Categories->find('list', [
    'limit' => 200,
    'keyField' => 'id',
    'valueField' => 'categoryName'
]);