cakephp2.1:HABTM saveAssociated和keep exists数据
我对saveAssociated和exists条目有点问题:( 型号:cakephp2.1:HABTM saveAssociated和keep exists数据,php,cakephp,cakephp-2.1,Php,Cakephp,Cakephp 2.1,我对saveAssociated和exists条目有点问题:( 型号: CREATE TABLE IF NOT EXISTS `ingredients` ( `id` int(11) NOT NULL AUTO_INCREMENT, `ingredient` varchar(250) NOT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY
CREATE TABLE IF NOT EXISTS `ingredients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ingredient` varchar(250) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `ingredient` (`ingredient`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `ingredients_recipes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ingredient_id` int(11) NOT NULL,
`recipe_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `recipes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`recipe` varchar(250) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `recipe` (`recipe`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
component.php
我的问题:
CREATE TABLE IF NOT EXISTS `ingredients` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ingredient` varchar(250) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `ingredient` (`ingredient`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `ingredients_recipes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ingredient_id` int(11) NOT NULL,
`recipe_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `recipes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`recipe` varchar(250) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `recipe` (`recipe`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
如何在“配方和配料”字段中保留现有数据,并继续在“配料和配料”字段中保存相关数据?
'unique'=>“keepExisting”,
它的作用是保留联接表中其他行的现有信息,但其作用仍然与'unique'=>true
相同
见:
您需要做的是将其设置为false。
'unique'=>'keepExisting',
它的作用是保留联接表中其他行的现有信息,但其作用仍然与'unique'=>true
相同
见:
您需要做的是将其设置为false。由于配方和配料记录已经存在,您应该能够获取它们的id。然后在保存数据时,您只需提供id来保存新关系,如
dataArray = array(
'recipe'=>array('id'=>$recipeId),
'ingredient'=>array('id'=>$ingredientId)
);
这样,配方
和成分
表中的记录就不会被修改或复制
如果表单中的输入是文本而不是下拉列表,则可能需要在保存关系之前手动查找id。由于配方和成分记录已经存在,您应该能够获取它们的id。然后在保存数据时,您只需提供id以保存新关系,如
dataArray = array(
'recipe'=>array('id'=>$recipeId),
'ingredient'=>array('id'=>$ingredientId)
);
这样,配方
和成分
表中的记录就不会被修改或复制
如果表单中的输入是文本,而不是下拉列表,则可能需要在保存关系之前手动查找id。在配方和配料模型中,make unique=false 从配方控制器开始
$this->Recipe->saveAll($this->request->data)
我认为这将在配方和配料模型中起作用 从配方控制器开始
$this->Recipe->saveAll($this->request->data)
我认为这将起作用没有任何变化…我变成错误“完整性约束冲突:1062重复条目”我看到您在相同的模型上使用了hasmany through joinmodel和habtm,这是错误的,从两个模型中选择一个。没有任何变化…我变成错误“完整性约束冲突:1062重复条目”我看到你在同一个模型上使用了hasmany-through-joinmodel和habtm,这是错误的,从两个模型中选择一个。
dataArray = array(
'recipe'=>array('id'=>$recipeId),
'ingredient'=>array('id'=>$ingredientId)
);
$this->Recipe->saveAll($this->request->data)