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.