CakePHP: saveAll with hasOne plus HABTM
I have two models that need to be saved in the same form. When a user uses the form "/ deliveries / add /" I need to save the new delivery and also save the new license attached to this delivery. This is a hasOne relationship.
Delivery belongsTo License
License hasOne Delivery
Also in the same form, I need to select which products and product variants the license includes:
License HABTM Products
License HABTM ProductOption
The problem I am having is that it seems that CakePHP is not detecting that my License / Products and License / ProductOptions relationships are HABTM, and the form helper only gives me a single dropdown and not a multiple choice dropdown. Even if I force it to be a multiple in the form helper, it still won't save the data and will not fill out the edit form correctly (even if it is typing the correct labels for the product parameters). I think it might have something to do with the fact that this is such a distant relationship from my main savings model?
I've posted the relevant code below. Thank you for watching this for me!
The shipping model looks like this:
class Delivery extends AppModel {
var $name = 'Delivery';
var $belongsTo = array('Company','Address','Contract','DeliveryType','License');
}
The license model looks like this:
class License extends AppModel {
var $name = 'License';
var $belongsTo = 'LicenseType';
var $hasOne = 'Delivery';
var $hasAndBelongsToMany = array('ProductOption','Product');
}
The supply controller looks like this:
function add() {
if (!empty($this->data)) {
if ($this->Delivery->saveAll($this->data)) {
$this->Session->setFlash(sprintf(__('The %s has been saved', true), 'delivery'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(sprintf(__('The %s could not be saved. Please, try again.', true), 'delivery'));
}
}
$companies = $this->Delivery->Company->find('list');
$addresses = $this->Delivery->Address->find('list');
$contracts = $this->Delivery->Contract->find('list');
$deliveryTypes = $this->Delivery->DeliveryType->find('list');
$licenses = $this->Delivery->License->find('list');
$licenseTypes = $this->Delivery->License->LicenseType->find('list');
$products = $this->Delivery->License->Product->find('list');
$productOptions = $this->Delivery->License->ProductOption->find('list');
$this->set(compact('companies', 'addresses', 'contracts', 'deliveryTypes', 'licenses','licenseTypes','products','productOptions'));
}
The views / supplies / add.ctp looks like this:
<div class="deliveries form">
<?= $form->create(); ?>
<fieldset>
<legend><?php printf(__('Add %s', true), __('Delivery', true)); ?></legend>
<?
echo $form->create();
echo $form->input('Delivery.company_id');
echo $form->input('Delivery.address_id');
echo $form->input('Delivery.contract_id');
echo $form->input('Delivery.delivery_date', array('dateFormat' => 'MDY', 'timeFormat' => 'none'));
echo $form->input('Delivery.serial_number');
echo $form->input('Delivery.description');
echo $form->input('Delivery.comments');
echo $form->input('Delivery.delivery_type_id');
echo $form->input('License.license_type_id');
echo $form->input('License.nodelocked');
echo $form->input('License.mac_addr');
echo $form->input('License.expiration_date', array('dateFormat' => 'MDY', 'timeFormat' => 'none'));
echo $form->input('License.Product');
echo $form->input('License.ProductOption');
?>
</fieldset>
<?= $form->end('Add');?>
</div>
a source to share
Is it possible to save data against the License model instead of the delivery model?
$this->Licence->saveAll($this-data);
Then we have:
echo $form->create('Licence');
...
echo $form->input('Product', array('multiple'=>true));
echo $form->input('ProductOption', array('multiple'=>true));
a source to share