I have these 3 tables:
customers:
services:
customerservices:
With this relation in CustomerservicesTable.php
:
$this->belongsTo('Customers')
->setForeignKey('customerid');
$this->belongsTo('Services')
->setForeignKey('serviceid');
In Template\Customerservices\add.ctp
I have a form with a dropdown and a numeric field :
<div class="customerservices form large-9 medium-8 columns content">
<?= $this->Form->create($customerservice) ?>
<fieldset>
<legend><?= __('Add transaction') ?></legend>
<?php
echo $this->Form->input('Transaction type',array('options' => $servicesList));
echo $this->Form->control('price');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
in Controller\CustomerservicesController.php
:
public function add($customerid = null)
{
$customerservice = $this->Customerservices->newEntity();
if ($this->request->is('post')) {
$customerservice->customerid = $customerid;
$customerservice->serviceid = //get selection from dropdown
if ($this->Customerservices->save($customerservice)) {
$this->Flash->success(__('The customerservice has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The customerservice could not be saved. Please, try again.'));
}
$this->set(compact('customerservice'));
$servicesList = TableRegistry::getTableLocator()->get('Services')->find('list');
$this->set(compact('servicesList'));
}
How can I replace the comment in order to save the serviceid
which is selected in the dropdown control?
(secondary question is it possible to hide the price
field depending on the dropdown selection?)