两个连接字段作为 valueField

two join fields as valueField

我正在尝试将两个连接字段合并为下拉组合中的 valueField。 检查 and 后,我尝试了以下操作:

这个有效:

$authorities = $this->Books->Authorities->find('list', [
            'valueField' => 'author.name'])
            ->contain(['Authors', 'AuthorTypes']);

这也有效:

$authorities = $this->Books->Authorities->find('list', [
            'valueField' => 'author_type.name'])
            ->contain(['Authors', 'AuthorTypes']);

但这不是:

$authorities = $this->Books->Authorities->find('list', [
            'valueField' => function ($row) {
                return $row['author.name'] . ' - ' . $row['author_type.name'];
            }])
        ->contain(['Authors', 'AuthorTypes']);

我不明白为什么。有任何想法吗? :)

按照其中一条评论的建议,遵循此 examples

您的代码应该是:

$authorities = $this->Books->Authorities->find('list', [
            'valueField' => function ($row) {
                return $row->author->name . ' - ' . $row->author->name;
            }])
            ->contain(['Authors', 'AuthorTypes']);

这叫做Customize Key-Value Output。 $row 参数是一个对象而不是简单的 array_object.