2
何らかの理由でHABTM関係を保存できません。フォームで生成されたデータ構造をチェックしてコントローラに渡し、モデル内のデータ構造をチェックして、すべてがinorderであるようです。どんな助けでも大歓迎です。Cakephp HABTMが保存しない
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)); ?>