2

我设置了两个模型,需要保存在一个表单中。当用户使用“/deliveries/add/”表单时,我需要它来保存新的交付,并保存附加到该交付的新许可证。这是一个 hasOne 关系。

Delivery belongsTo License
License hasOne Delivery

同样在同一个表格中,我需要选择许可证包括哪些产品和产品选项:

License HABTM Products
License HABTM ProductOption

我遇到的问题是,CakePHP 似乎没有检测到我的许可证/产品和许可证/产品选项关系是 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>
4

1 回答 1

0

是否可以根据许可证模型而不是交付模型保存数据?

$this->Licence->saveAll($this-data);

然后有:

echo $form->create('Licence');
...
echo $form->input('Product', array('multiple'=>true));
echo $form->input('ProductOption', array('multiple'=>true));
于 2011-02-10T11:13:18.070 回答