Reputation: 73
For some reason i am unable to save HABTM relationships, I have checked the data structure generated by the form and passed to the controller and also check the data structure in the model, and all seems to be inorder. Any help would greatly be appreciated.
CREATE TABLE `products_colors` (
`product_id` char(36) NOT NULL DEFAULT '',
`color_id` char(36) NOT NULL DEFAULT '',
PRIMARY KEY (`product_id`,`color_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `products_sizes` (
`product_id` char(36) NOT NULL,
`size_id` char(36) NOT NULL DEFAULT '',
PRIMARY KEY (`product_id`,`size_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
class ProductsController extends AppController {
function edit($id = null)
{
if(!is_null($id))
{
$this->set('productCategories', $this->Product->Category->find('list'));
$this->set('sizes', $this->Product->Sizes->find('list'));
$this->set('colors', $this->Product->Colors->find('list'));
$this->Product->contain(array('Sizes', 'Colors'));
$product = $this->Product->read(null, $id);
if(!empty($product)) {
if($this->RequestHandler->isPut()) {
$this->Product->id = $id;
$this->Product->begin();
$this->Product->set($this->data);
debug($this->data); die;
//$dbo = $this->Product->getDatasource();
//$dbo->fullDebug = true;
if($this->Product->save()) {
$this->Product->commit();
//debug($dbo->_queriesLog); die;
$this->Session->setFlash( __('Product updated',true), 'flash_true');
$this->redirect(array('action' => 'view', $this->Product->field('slug')));
} else {
$this->Product->rollback();
$this->Session->setFlash(__('Please correct the errors below',true), 'flash_false');
}
} else {
$this->data = $product;
}
$this->render('_form');
return;
}
}
$this->redirect(array('controller' => 'categories', 'action' => 'index'));
}
}
class Product extends AppModel
{
var $hasAndBelongsToMany = array(
'Colors' => array(
'className' => 'Color',
'joinTable' => 'products_colors',
'foreignKey' => 'product_id',
'associationForeignKey' => 'color_id',
'unique' => false,
),
'Sizes' => array(
'className' => 'Size',
'joinTable' => 'products_sizes',
'foreignKey' => 'product_id',
'associationForeignKey' => 'size_id',
),
);
}
<?php echo $this->Form->create('Product'); ?>
<fieldset>
<legend><?php __('Options') ?></legend>
<?php echo $this->Form->input('Product.id') ?>
<?php echo $this->Form->input('Product.product_category_id', array('label' => __('Category', true))) ?>
<?php echo $this->Form->input('Sizes', array('label' => __('Sizes', true), 'multiple' => 'checkbox')) ?>
<?php echo $this->Form->input('Colors', array('label' => __('Colors', true), 'multiple' => 'checkbox')) ?>
</fieldset>
<fieldset>
<legend><?php __('Product') ?></legend>
<?php echo $this->Form->input('Product.sku') ?>
<?php echo $this->Form->input('Product.price') ?>
<?php echo $this->Form->input('Product.name') ?>
<?php echo $this->Form->input('Product.description') ?>
<?php echo $this->Form->input('Product.state', array('type' => 'checkbox')) ?>
</fieldset>
<?php echo $this->Form->end(__('Submit', true)); ?>
Upvotes: 2
Views: 719
Reputation: 73
For anyone else facing this issue, Cakephp only supports id's that are either INT() or CHAR(36). Using a non standard ID will result in a similar issue.
Upvotes: 5