【发布时间】:2015-12-11 23:04:10
【问题描述】:
上下文:
我在 CakePHP 3.0 中做了一个有点复杂的嵌套连接:
$query = $this->find()
->contain([
'A',
'A.B',
'A.B.C',
'A.B.C.D',
'A.B.C.D.Z'
]);
A、B、C、D、Z之间的关系如下:
-
BhasManyA -
BhasManyC -
ChasManyB -
ChasManyD -
DhasManyC -
ZhasManyD
在contain() 数组中,我可以执行A、A.B、A.B.C 和A.B.C.D,但不能 A.B.C.D.Z。给出的错误信息是:
D is not associated with Z (InvalidArgumentException)
Could this be caused by using AutoTables?
Please try correcting the issue for the following table aliases:
• D
我没有发现任何对D 的拼写错误的引用。
问题:
如何在此加入语句中包含Z?
代码:
我的C 模特:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class CTable extends Table
{
public function initialize(array $config)
{
$this->table('c');
$this->primaryKey('id');
$this->hasMany('D', [
'className' => 'D',
'foreignKey' => false,
'conditions' => ['D.id' => 'C.d_id']
]);
$this->hasMany('B', [
'className' => 'B',
'foreignKey' => false,
'conditions' => ['B.id' => 'C.b_id']
]);
}
}
我的D 模特:
<?php
namespace App\Model\Table
use Cake\ORM\Table;
class DTable extends Table
{
public function initialize(array $config)
{
$this->table('z');
$this->primaryKey('id');
$this->hasMany('C', [
'className' => 'C',
'foreignKey' => 'd_id'
]);
$this->belongsTo('Z', [
'className' => 'Z',
'foreignKey' => 'z_id'
]);
}
}
我的Z 模特:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class ZTable extends Table
{
public function initialize(array $config)
{
$this->table('z');
$this->primaryKey('id');
$this->hasMany('D', [
'className' => 'D',
'foreignKey' => 'z_id'
]);
// Unrelated to the join statement, but in here nevertheless
$this->belongsTo('X', [
'className' => 'X',
'foreignKey' => 'x_id',
]);
}
}
数据库:
我的C 表列:
id | b_id | d_id
我的D 表列:
id | z_id | data
我的Z 表列:
id | x_id | data1 | data2 | data3 | data4 | data5
【问题讨论】:
标签: sql-server cakephp join cakephp-3.0