加入独立的表格

时间:2016-01-20 05:23:47

标签: cakephp cakephp-3.0

我正在使用Cakephp 3.x. 如果我说我有表A,B,C和D.

表C'属于'A和B. C包含A和B的外键。
表D'属于'A和B.D包含A和B的外键,与C相同。

现在我想在表C和D中一起显示信息。 我怎样才能做到这一点。 谢谢。

$tableReg1 = TableRegistry::get('C');
$results1 = $tableReg1->find();
$results1->select([
    'id', 'A.name', 'B.number',
    'total' => $results1->func()->sum('amount')
])
->group('A.name')
->order(['A.name' => 'ASC'])
->contain(['A', 'B']);

如果让我说我在这部分包括D - >包含(['A','B','D']); 它将返回错误:C与D无关。

在表C和表D中,我只声明了这一点。

public function initialize(array $config)
{
    parent::initialize($config);

    $this->belongsTo('A', [
        'foreignKey' => 'tableAid',
        'joinType' => 'INNER'
        //'through' => 'Selections'
    ]);

    $this->belongsTo('B', [
        'foreignKey' => 'tableBid',
        'joinType' => 'INNER'
    ]);

    $this->table('C');
    $this->displayField('id');
    $this->primaryKey('id');
}

0 个答案:

没有答案