1つのフォーム内に保存する必要がある2つのモデルを設定しています。ユーザーが「/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');
}
deliveryies_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 / deliverys/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>