Cakephp 3.x: 通过关联访问变量 table

Cakephp 3.x: accessing variable through associated table

我有一笔费用 table 有相关的年度运营预算 table。

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

该年度运营预算 Table 与 AZ 机构 table 相关联。

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

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

您可以使用 containable

检索机构详细信息以及 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
      ... }