Sql server 使用belongsTo在嵌套表上连接CakePHP 3.0 背景:

Sql server 使用belongsTo在嵌套表上连接CakePHP 3.0 背景:,sql-server,cakephp,join,cakephp-3.0,Sql Server,Cakephp,Join,Cakephp 3.0,我在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之间的关系如下: B有许多A B有许多C C有许多B C有许多D D有许多C Z有许多D 在contain()数组中,我可以执行A、A.B、A

我在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
之间的关系如下:

  • B
    有许多
    A
  • B
    有许多
    C
  • C
    有许多
    B
  • C
    有许多
    D
  • D
    有许多
    C
  • Z
    有许多
    D
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
的任何拼写错误的引用


问题: 如何在此join语句中包含
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']
        ]);
    }
}
<?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'
        ]);
   }
}
<?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',
        ]);
    }
}
My
Z
车型:

<?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']
        ]);
    }
}
<?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'
        ]);
   }
}
<?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',
        ]);
    }
}
我的
D
表格列:

id | b_id | d_id
id | z_id | data
id | x_id | data1 | data2 | data3 | data4 | data5
我的
Z
表格列:

id | b_id | d_id
id | z_id | data
id | x_id | data1 | data2 | data3 | data4 | data5

您的文件需要分别命名为
DTable.php
CTable.php
和class
class DTable extensed Table

Woops,这是我的错误。类和文件名确实是
Dtable.php
classdtable
等等。我编辑了我的问题来演示这一点。我想补充一点,当我尝试加入
Z
时,我的麻烦来了,除此之外什么都没有(它与我的所有其他加入一起工作)。这会是一个案例问题吗
Dtable.php
VS
Dtable.php
不太可能。表名不是字面意义上的
A
B
等。文件名是正确的。