Cakephp HABTM 不保存
由于某种原因,我无法保存 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)); ?>
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)); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于面临此问题的其他人,Cakephp 仅支持 INT() 或 CHAR(36) 的 id。使用非标准 ID 也会导致类似的问题。
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.