【问题标题】:CakePHP 3.0 join on nested table with belongsToCakePHP 3.0 使用 belongsTo 连接嵌套表
【发布时间】: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'
    ]);

ABCDZ之间的关系如下:

  • BhasMany A
  • BhasMany C
  • ChasMany B
  • ChasMany D
  • DhasMany C
  • Z hasMany D

contain() 数组中,我可以执行AA.BA.B.CA.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


    【解决方案1】:

    您的文件需要分别命名为DTable.phpCTable.php 和类class DTable extends Table

    【讨论】:

    • 糟糕,那是我的错误。类和文件名确实是Dtable.phpclass DTable 等。我已经编辑了我的问题来证明这一点。我想补充一点,我在尝试加入 Z 时遇到了麻烦,仅此而已(它适用于我的所有其他加入)。
    • 可能是案例问题吗? Dtable.phpVS DTable.php
    • 不太可能。表名不是字面意思 AB 等。文件名是正确的StudlyCased。
    【解决方案2】:

    据我所知,问题是通过“hasMany”关系在 C 和 D 之间进行递归。 也许在 C 中重命名这种关系如下:

    $this->hasMany('aliasForD', [
            'className' => 'D',
            'foreignKey' => false,
            'conditions' => ['aliasForD.id' => 'C.d_id']
    ]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-28
      • 2015-07-20
      • 1970-01-01
      相关资源
      最近更新 更多