drbfxb977777 2015-09-21 15:53
浏览 40

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

I'm trying to use the same dropdown twice and have it work when storing the record in Cake 3.

The referenced table in this case is 'responsible_people' and the referencing table is 'organisation_details'. The FK in the latter is 'responsible_people_id'. Nothing special going on. If I bake it as it is, it's fine. I change the displayField in ResponsiblePeopleTable.php to 'full_name' and away we go, a dropdown as expected.

Does anyone know how I can extend this and have, say, two fields on the Org Details add page populated by the responsible people table and have it save correctly?

This is the controller's add section:

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']);
}

This is the 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() ?>

The last form input produces a nice dropdown with the names of the responsible people ready for selection. I'd like to work out how to have two of those.

  • 写回答

1条回答 默认 最新

  • douzhe2981 2015-09-22 20:39
    关注

    At the moment you have only one dropdown on your form. If you set name of your dropdowns then when you submit your form you will get values of each drop down.

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

    The other option to create a dropdown would be using Form->select(). e.g.

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

    Please check here for more information about Cakephp3.x Form Helper

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。