【问题标题】:Custom finder selecting fields in associated model Cakephp3自定义查找器选择关联模型 Cakephp3 中的字段
【发布时间】:2015-06-23 14:27:26
【问题描述】:

我有一个 Event 模型 hasMany Attendances,出席者中有 event_id。我想在我的自定义查找方法中选择出勤表中的一些字段,但是 contains() 方法没有加入这两个表。

public function findAttendanceDetails(Query $query) 
{
    return $query->contain('Attendances')
                ->select(['Gender' => 'Attendances.gender',
                            'Name' => 'Attendances.full_name'
                            ]);
}

我收到错误错误:SQLSTATE[42S22]: Column not found: 1054 Champ 'Attendances.gender' inconnu dans field list 并想知道缺少什么。

当我使用 ->Join() 方法而不是 ->contain() 时,我得到了结果。

【问题讨论】:

    标签: mysql cakephp-3.0 query-builder


    【解决方案1】:

    Contain 并不是这样工作的。你会寻找类似的东西:

    return $query->contain(['Attendances']);
    

    这只会抓取并返回完整的关联表出勤率。包含查找数组,而不是字符串值。

    如果您想要关联表中的特定字段,您可以尝试:

    return $query->contain(['Attendances' => function ($q) {
        return $q
            ->select(['gender', 'full_name'])
        }
    ]);
    

    您可以阅读有关使用 contains here 的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多