由于某种原因,我无法保存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)); ?>
答案 0 :(得分:5)
对于遇到此问题的任何其他人,Cakephp仅支持INT(INT)或者CHAR(36)。使用非标准ID会导致类似的问题。