【问题标题】:Cakephp 3: showing two values on form drop-down from associated tableCakephp 3:在关联表的表单下拉列表中显示两个值
【发布时间】:2018-03-22 15:46:51
【问题描述】:

我有三张桌子:

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>

【问题讨论】:

    标签: cakephp cakephp-3.x


    【解决方案1】:

    您可以使用计算字段

    $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]);
    

    【讨论】:

    • 虚拟财产表现出色。非常感谢阿里利亚
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 2015-01-29
    • 2020-06-20
    相关资源
    最近更新 更多