Friday, January 25, 2013

CakePHP 2.2.5 HABTM Edit view not showing previously selected values as selected.

Hey Guys, 

First of all, a warm hello to everybody here, secondly, i would want to know if somebody can help me with this problem.

This is the association part from the Product model, which correlates the Products table with the shipping_methods table, through a join table, called products_shipping_methods.


'ShippingMethod' => array(
'className' => 'ShippingMethod',
'joinTable' => 'products_shipping_methods',
'foreignKey' => 'product_id',
'associationForeignKey' => 'shipping_method_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => array('id','name'),
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);

admin_edit function, from ProductsController.php:

public function admin_edit($id = null) {
$this->Product->id = $id;
if (!$this->Product->exists()) {
throw new NotFoundException(__('Invalid product'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Product->save($this->request->data)) {
$this->Session->setFlash(__('The product has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Product->read(null, $id);
}
$stores = $this->Product->Store->find('list');
$categories = $this->Product->Category->find('list');
$currencies = $this->Product->Currency->find('list');
$attributes = $this->Product->Attribute->find('list');
$shippingMethods = $this->Product->ShippingMethod->find('list',array('fields'=>array('id','name')));
$this->set(compact('stores', 'categories', 'currencies', 'attributes', 'shippingMethods'));
}

View code:



<?php echo $this->Form->input('ShippingMethod',array('label'=>false,'multiple'=>'multiple','class'=>'chzn-select validate[required]','div'=>false,'data-placeholder'=>'Shipping methods'));?>

Please note that i've also tried naming the field ShippingMethod.ShippingMethod, without any luck tho'.

I have also tried to specify the 'selected' option both like this:
<?php echo $this->Form->input('ShippingMethod',array('label'=>false,'multiple'=>'multiple','selected'=>$this->Form->data['ShippingMethod'],'class'=>'chzn-select validate[required]','div'=>false,'data-placeholder'=>'Shipping methods'));?>
and like this, which when i hardocde it works, shows them selected: 

'selected'=>array(Hash::extract($this->Form->data['ShippingMethod'],'{n}.id')))

Example, if i make it 'selected'=>array(0=>1,1=>2), it works perfectly...

Please note that the Hash::extract gives me something like this:

Array
(
[0] => 1
[1] => 2
)



I would appreciate any kind of help i could get, to solve this in a clean manner without doing loops and so on, like foreach, custom $counter variable, if there is ANY way...please let me know if somebody had this problem before, how did you solve it?

Kind regards,

Alex.

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: