Focus on IT Recommend

Home > has and belongs to many - CakePHP: saveAll with hasOne plus a HABTM

has and belongs to many - CakePHP: saveAll with hasOne plus a HABTM



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:

Recommend:has and belongs to many - Cakephp retrieve data in HABTM association

BTM. Now here particular users are associated to distribution list, i wish to add more users, but when adding new users to distribution list, i would like to show users who are not associated with that distribution lists. what condition sho

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(); ?>
   <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');
<?= $form->end('Add');?>
cakephp has-and-belongs-to-many multiple-models has-one
  this question
edited Dec 16 '10 at 10:02 Thorpe Obazee 8,707 6 55 84 asked May 10 '10 at 18:51 Dan 119 3 12


1 Answers

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


Then have:

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

  this answer
answered Feb 10 '11 at 11:13 Botman 81 4


Recommend:has and belongs to many - CakePHP find HABTM

e Message belongsto User Message HABTM Tag Tag HABTM Message If a User is logged in he might want to see all Message tagged with something. $messages = $this->Message->find('all', array( 'conditions' => array("Message.user_id" => $this-

------splitte line----------------------------