我有三张桌子:
BudgetRevenues - 属于AnnualOperatingBudgets
AnnualOperatingBudgets有一个Azinstitutions
在我的预算收入添加表格中,第一个选择是针对预算年度&机构。
public function add()
{
$budgetRevenue = $this->BudgetRevenues->newEntity();
$associated = [ 'RevenueTitles', 'AnnualOperatingBudgets', 'AnnualOperatingBudgets.Azinstitutions'];
if ($this->request->is('post')) {
$budgetRevenue = $this->BudgetRevenues->patchEntity($budgetRevenue, $this->request->getData(), ['associated' => $associated]);
if ($this->BudgetRevenues->save($budgetRevenue)) {
$this->Flash->success(__('The budget revenue has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The budget revenue could not be saved. Please, try again.'));
}
$annualOperatingBudgets = $this->BudgetRevenues->AnnualOperatingBudgets->find('list', ['keyField' => 'id', 'valueField' => ['budget_year', 'azinstitutions_id']]);
$revenueTitles = $this->BudgetRevenues->RevenueTitles->find('list', ['keyField' => 'id', 'valueField' => 'revenue_title']);
$this->set(compact('budgetRevenue', 'revenueTitles', 'annualOperatingBudgets', 'AnnualOperatingBudgets.Azinstitutions'));
}
在我的代码中,我可以调用预算年度(AnnualOperatingBudgets中的字段)和azinstitution_id。但我希望它能显示该机构的名称。这是在由AnnualOperatingBudgets表中的azinstitution_id链接的azinstitutions表中。
现在表格显示2015; 1 我希望它能展示2015 ASU
我的add.ctp看起来像这样:
<div class="budgetRevenues form large-9 medium-8 columns content">
<?= $this->Form->create($budgetRevenue) ?>
<fieldset>
<legend><?= __('Add Budget Revenue') ?></legend>
<?php
echo $this->Form->control('annual_operating_budget_id', ['label' => 'Budget Year & Institution'], ['options' => $annualOperatingBudgets ]);
echo $this->Form->control('revenue');
echo $this->Form->control('revenue_title_id', ['options' => $revenueTitles]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
答案 0 :(得分:2)
你可以使用计算字段
$query = $this->BudgetRevenues->AnnualOperatingBudgets->find();
$annualOperatingBudgets = $query
->find('list', [
'keyField' => 'id',
'valueField' => 'full_year'
])
->select([
'id',
'full_year' => $query->func()->concat([
'budget_year' => 'identifier',
' - ',
'Azinstitutions.name' => 'identifier'
])
])
->contain(['Azinstitutions']);
另一种解决方案是在您的实体中使用虚拟财产
在您的AnnualOperatingBudget
实体中创建虚拟财产
// AnnualOperatingBudget.php
public function _getFullYear()
{
return $this->budget_year.' - '.$this->azinstitution->name;
// You should add a check to ensure
// that $this->azinstitution actually exists
}
//controller
$annualOperatingBudgets = $this->BudgetRevenues->AnnualOperatingBudgets
->find('list', ['keyField' => 'id', 'valueField' => 'full_year'])
->contain(['Azinstitutions]);