CakePHP:使用 hasOne 和 HABTM 进行 saveAll

发布于 2024-08-31 20:17:49 字数 3366 浏览 4 评论 0原文

我设置了两个模型,需要将其保存在一个表单中。当用户使用“/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');
}

所述交付控制器如下所示:

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>

I have two models set up that I need to save inside one form. When the user uses the "/deliveries/add/" form, I need it to save a new delivery, and also save a new license that is attached to that 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 options the license includes:

License HABTM Products
License HABTM ProductOption

The problem I'm running into is that it seems CakePHP does not detect that my License/Products and License/ProductOptions relationships are HABTM, and the form helper only gives me a single select dropdown instead of a multiple select dropdown. Even if I force it to be a multiple in the form helper, it still will not save the data, nor will it populate an edit form correctly (even though it pulls in the correct labels for the product options). I think this may have something to do with it being such a distant relationship from my main saving model?

I've posted the relevant code below. Thank you anyone for looking over this for me!

The delivery 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 deliveries_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/deliveries/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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2024-09-07 20:17:49

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

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

然后有:

echo $form->create('Licence');
...
echo $form->input('Product', array('multiple'=>true));
echo $form->input('ProductOption', array('multiple'=>true));

Would it be possible to save the data against the License model instead of the Delivery model?

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

Then have:

echo $form->create('Licence');
...
echo $form->input('Product', array('multiple'=>true));
echo $form->input('ProductOption', array('multiple'=>true));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文