cakephp 3使用包含在子关联表中

时间:2018-01-18 10:05:58

标签: php mysql cakephp cakephp-3.0

我有4个表,提案,投标,用户和驱动程序。提案有很多出价和出价有很多用户,最后司机属于用户。

我尝试做的是获取提案1的所有出价,提案1应显示提案名称,出价编号,出价的用户详细信息,包括驱动程序表中包含用户特定详细信息的字段。

我的架构如下:

{
    name: 'my object',
    description: 'it is an object',
    details: {
        it: 'has',
        an: 'array',
        with: ['a', 'few', 'elements']
    }
}

以下是我正在使用的代码失败 - 我知道我错了:

{
    name: 'updated object',
    description: 'it is an object!',
    details: {
        it: 'has',
        an: 'array',
        with: ['a', 'few', 'more', 'elements']
    }
}

任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

我用以下内容解决了这个问题:

 $proposal = $this->Proposals->get($id, [
        'contain' => ['Bids' => ['Users' => ['Drivers']]]

    ]);

答案 1 :(得分:1)

您也可以使用点符号来遍历关系

$proposal = $this->Proposals->get($id, [
    'contain' => ['Bids.Users.Drivers']
]);

https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#eager-loading-associations-via-contain