由一个相关表CakePHP 3填充的多个下拉列表

时间:2015-09-21 15:53:39

标签: php mysql cakephp

我正在尝试使用相同的下拉菜单两次,并在将蛋糕3中的记录存储时使其正常工作。

在这种情况下引用的表是'responsible_people',引用表是'organisation_details'。后者的FK是'responsible_people_id'。没什么特别的。如果我按原样烘烤它,那很好。我将ResponsiblePeopleTable.php中的displayField更改为'full_name'然后我们去了,按预期下拉。

有没有人知道我如何扩展这个,并且在负责人表填充的组织详细信息添加页面上有两个字段并正确保存?

这是控制器的添加部分:

public function add()
{
    $organisationDetail = $this->OrganisationDetails->newEntity();
    if ($this->request->is('post')) {
        $organisationDetail = $this->OrganisationDetails->patchEntity($organisationDetail, $this->request->data);
        if ($this->OrganisationDetails->save($organisationDetail)) {
            $this->Flash->success(__('The organisation detail has been saved.'));
            return $this->redirect(['action' => 'index']);
        } else {
            $this->Flash->error(__('The organisation detail could not be saved. Please, try again.'));
        }
    }
    $responsiblePeople = $this->OrganisationDetails->ResponsiblePeople->find('list', ['limit' => 200]);
    $this->set(compact('organisationDetail', 'responsiblePeople'));
    $this->set('_serialize', ['organisationDetail']);
}

这是add.ctp:

<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
    <li class="heading"><?= __('Actions') ?></li>
    <li><?= $this->Html->link(__('List Organisation Details'), ['action' => 'index']) ?></li>
    <li><?= $this->Html->link(__('List Responsible People'), ['controller' => 'ResponsiblePeople', 'action' => 'index']) ?></li>
    <li><?= $this->Html->link(__('New Responsible Person'), ['controller' => 'ResponsiblePeople', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="organisationDetails form large-9 medium-8 columns content">
<?= $this->Form->create($organisationDetail) ?>
<fieldset>
    <legend><?= __('Add Organisation Detail') ?></legend>
    <?php
        echo $this->Form->input('organisation_name');
        echo $this->Form->input('organisation_address');
        echo $this->Form->input('organisation_secondary_addresses');
        echo $this->Form->input('organisation_email');
        echo $this->Form->input('organisation_telephone');
        echo $this->Form->input('organisation_employees');
        echo $this->Form->input('organisation_contractors');
        echo $this->Form->input('organisation_review', ['empty' => true, 'default' => '']);
        echo $this->Form->input('organisation_external_assessment', ['empty' => true, 'default' => '']);
        echo $this->Form->input('responsible_people_id', ['options' => $responsiblePeople]);
    ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>

最后一个表单输入会生成一个很好的下拉列表,其中有负责人员的名字可供选择。我想弄清楚如何有两个。

1 个答案:

答案 0 :(得分:1)

目前您的表单上只有一个下拉列表。 如果您设置了下拉列表的名称,那么当您提交表单时,您将获得每个下拉列表的值。

echo $this->Form->input('responsible_people_id1', ['name'=>'dropdown1', 'options' => $responsiblePeople]);

echo $this->Form->input('responsible_people_id2', ['name'=>'dropdown2', 'options' => $responsiblePeople]);

创建下拉列表的另一个选项是使用Form-&gt; select()。 e.g。

echo $this->Form->select(
    'field',
    [1, 2, 3, 4, 5],
    ['empty' => '(choose one)']
);

请点击此处查看有关Cakephp3.x Form Helper

的更多信息