views:

24

answers:

0

Hello everyone.

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>