【问题标题】:two join fields as valueField两个连接字段作为 valueField
【发布时间】:2018-08-07 04:57:56
【问题描述】:

我正在尝试将两个连接字段组合为下拉组合中的 valueField。 检查https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairsHow do I create a keyValue pair (display field) by combining/having two fields in CakePHP 3? 后,我尝试了以下操作:

这行得通:

$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']);

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

【问题讨论】:

  • 按照例子here应该是$row->author->name
  • 我一定是瞎了眼……因为现在我看不到它是同一个例子^_ ^!

标签: php cakephp join field


【解决方案1】:

按照其中一位 cmets 的建议,请关注 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。

【讨论】:

    猜你喜欢
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 1970-01-01
    相关资源
    最近更新 更多