【问题标题】:How to use associated data with the list finder?如何将关联数据与列表查找器一起使用?
【发布时间】:2019-11-15 07:58:26
【问题描述】:

我正在使用带有列表的查找为我的下拉菜单创建选项。 但我想获取关联表的数据。 下面给出的是我的代码

$users = $this->UserRoles
    ->find('list', [
        'contain' => ['Users'],
        'limit' => 200,
        'keyField' => 'id',
        'valueField' => 'Users.first_name'
    ])
    ->where([
        'Users.status' => true,
        'Users.company_id' => $company_id,
        'UserRoles.role_id' => 4
    ]);

但它返回 null 作为输出。

【问题讨论】:

  • 每当您遇到关联问题时,请同时解释关联设置,我只能假设它是UserRoles belongsTo Users。另外,请更具体地说明null 值究竟出现在哪里,是$usersnull,还是列表中的键和/或值null,还是...?

标签: php cakephp associations query-builder cakephp-3.x


【解决方案1】:

本书中的示例将contain 查询链接如下:

$query = $articles->find('list', [ 'keyField' => 'id', 'valueField' => 'author.name' ])->contain(['Authors']);

所以不妨试试:

$users = $this->UserRoles ->find('list', [ 'limit' => 200, 'keyField' => 'id', 'valueField' => 'Users.first_name' ]) ->contain(['Users']) ->where([ 'Users.status' => true, 'Users.company_id' => $company_id, 'UserRoles.role_id' => 4 ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2014-03-15
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    相关资源
    最近更新 更多