Cakephp 3.x:通过关联表

时间:2018-02-20 23:47:55

标签: cakephp associations

我有一张费用表,其中包含相关的年度运营预算表。

$this->belongsTo('AnnualOperatingBudgets', [
            'foreignKey' => 'annual_operating_budgets_id'
        ]);

该年度运营预算表与AZinstitution表相关联。

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

当我查看费用时,我可以将foreignKey显示到年度运营预算表中的机构表,但我想显示与机构表关联的机构名称。那可能吗?或者我还需要将'azinstitutions_id'作为外键添加到我的费用表中。

1 个答案:

答案 0 :(得分:0)

您可以使用可包含的

来检索Azinstitutions详细信息以及AnnualOperatingBudgets

来自ExpensesController.php

public function view($id)
{
     $expense = $this->Expenses->findById($id)
        ->contain('AnnualOperatingBudgets.Azinstitutions')
        ->first();
     //get institution name from $expense->annual_operating_budget->azinstitution->name
      ... }