在cakephp 3.x

时间:2018-01-24 17:23:37

标签: cakephp-3.0

我有一个'飞行'实体,其中包含2个'belongsToHasMany'依赖项(一个是'crw' - 一个机组成员和'代理' - 一个机组成员类型,例如,'教练'或'安全飞行员')。 所有控制器和视图都是通过'bake'实用程序创建的,所有CRUD操作都按预期工作。

我需要更改为'crw'和'actingas'创建默认下拉选项的方式。现在他们多选,我需要让他们单选,所以我可以添加'crw-actingas'的几种组合。

我查看了几本较旧的教程,但老实说无处可去。

我尝试了'飞行'的'编辑'视图。 生成的字段集如下所示:

    <fieldset>
    <legend><?= __('Edit Flight') ?></legend>
    <?php
        echo $this->Form->control('date');
        echo $this->Form->control('total_duration_of_flight');
        echo $this->Form->control('actingas._ids', ['options' => $actingas]);
        echo $this->Form->control('crw._ids', ['options' => $crw]);
    ?>
</fieldset>

...所以在摆弄它之后复制并粘贴生成的html(更改控件的名称):

    <div class="input select">
    <label for="actingas-ids">Actingas</label>
    <input type="hidden" name="actingas[_ids]" value="">
    <select name="actingas[_ids][]" multiple="multiple" id="actingas-ids-0">
        <option value="1">CFI</option>
        <option value="2">Safety Pilot</option>
        <option value="3">CFII</option>
        <option value="4">MEI</option>
    </select>
</div>


<div class="input select">
    <label for="crw-ids">Crw</label>
    <input type="hidden" name="crw[_ids]" value="">
    <select name="crw[_ids][]" multiple="multiple" id="crw-ids-0">
        <option value="2">Rob</option>
        <option value="3">Tim</option>
        <option value="4">Bob</option>
        <option value="5">Rog</option>
        <option value="6">Bart</option>
    </select>
</div>

...当然,自动生成的选择中的值会被保存,但不是hacky的值。但我也没有任何错误。

'FlightController'中的'edit'功能:

 public function edit($id = null)
{
    $flight = $this->Flight->get($id, [
        'contain' => ['Actingas', 'Crw']
    ]);
    if ($this->request->is(['patch', 'post', 'put'])) {

        //var_dump( $this->request->getData() );
        //phpinfo();
        //die();

        $flight = $this->Flight->patchEntity($flight, $this->request->getData());
        if ($this->Flight->save($flight)) {
            $this->Flash->success(__('The flight has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The flight could not be saved. Please, try again.'));
    }
    $actingas = $this->Flight->Actingas->find('list', ['limit' => 200]);
    $crw = $this->Flight->Crw->find('list', ['limit' => 200]);
    $this->set(compact('flight', 'actingas', 'crw'));
}

航班表:

    class FlightTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('flight');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->belongsToMany('Actingas', [
            'foreignKey' => 'flight_id',
            'targetForeignKey' => 'actingas_id',
            'joinTable' => 'flight_actingas'
        ]);
        $this->belongsToMany('Crw', [
            'foreignKey' => 'flight_id',
            'targetForeignKey' => 'crw_id',
            'joinTable' => 'flight_crw'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->dateTime('date')
            ->requirePresence('date', 'create')
            ->notEmpty('date');

        $validator
            ->numeric('total_duration_of_flight')
            ->requirePresence('total_duration_of_flight', 'create')
            ->notEmpty('total_duration_of_flight');

        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        return $validator;
    }
}

Actingas table:

    class ActingasTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('actingas');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->belongsToMany('Crw', [
            'foreignKey' => 'actingas_id',
            'targetForeignKey' => 'crw_id',
            'joinTable' => 'crw_actingas'
        ]);
        $this->belongsToMany('Flight', [
            'foreignKey' => 'actingas_id',
            'targetForeignKey' => 'flight_id',
            'joinTable' => 'flight_actingas'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->scalar('title')
            ->allowEmpty('title');

        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        return $validator;
    }
}

CRW表:

    class CrwTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('crw');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->belongsToMany('Actingas', [
            'foreignKey' => 'crw_id',
            'targetForeignKey' => 'actingas_id',
            'joinTable' => 'crw_actingas'
        ]);
        $this->belongsToMany('Flight', [
            'foreignKey' => 'crw_id',
            'targetForeignKey' => 'flight_id',
            'joinTable' => 'flight_crw'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->scalar('title')
            ->maxLength('title', 255)
            ->allowEmpty('title');

        $validator
            ->integer('id')
            ->allowEmpty('id', 'create');

        return $validator;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为只是

echo $this->Form->control('actingas._ids', [
    'options' => $actingas, 
    'multiple' => false,
    'type' => 'select'
]);

应该有效