我设置了两个模型,我需要在一个表单中保存。当用户使用“/ deliveries / add /”表单时,我需要它来保存新的交付,并且还保存附加到该交付的新许可证。这是一个hasOne关系。
Delivery belongsTo License
License hasOne Delivery
同样,我需要选择许可证包含的产品和产品选项:
License HABTM Products
License HABTM ProductOption
我遇到的问题是,似乎CakePHP没有检测到我的许可证/产品和许可证/ ProductOptions关系是HABTM,并且表单助手只给我一个选择下拉列表而不是多选择下拉列表。即使我强制它在表单助手中是一个倍数,它仍然不会保存数据,也不会正确填充编辑表单(即使它为产品选项提供了正确的标签)。我认为这可能与我的主要储蓄模式之间存在如此遥远的关系有什么关系?
交付模式如下:
class Delivery extends AppModel {
var $name = 'Delivery';
var $belongsTo = array('Company','Address','Contract','DeliveryType','License');
}
许可证模型如下所示:
class License extends AppModel {
var $name = 'License';
var $belongsTo = 'LicenseType';
var $hasOne = 'Delivery';
var $hasAndBelongsToMany = array('ProductOption','Product');
}
deliveries_controller看起来像这样:
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'));
}
views / deliveries / add.ctp如下所示:
<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>
答案 0 :(得分:0)
是否可以根据许可证模型而不是交付模式保存数据?
$this->Licence->saveAll($this-data);
然后:
echo $form->create('Licence');
...
echo $form->input('Product', array('multiple'=>true));
echo $form->input('ProductOption', array('multiple'=>true));