为什么CakePHP中的create函数试图保存以前的条目?导致重复输入错误
我是CakePHP新手,在使用create函数添加新行时出现以下错误: 完整性约束冲突:密钥“PRIMARY”的1062个重复条目“0-0” 每当我使用Formhelper保存新数据时,Profile.id设置为0,但已经有一行具有该id。User.id和Profile.id都设置为自动递增 我的问题是:如何使用下一个可用id保存数据 模型如下:为什么CakePHP中的create函数试图保存以前的条目?导致重复输入错误,php,mysql,sql,cakephp,Php,Mysql,Sql,Cakephp,我是CakePHP新手,在使用create函数添加新行时出现以下错误: 完整性约束冲突:密钥“PRIMARY”的1062个重复条目“0-0” 每当我使用Formhelper保存新数据时,Profile.id设置为0,但已经有一行具有该id。User.id和Profile.id都设置为自动递增 我的问题是:如何使用下一个可用id保存数据 模型如下: class Profile extends AppModel { public $name='Profile'; public $be
class Profile extends AppModel {
public $name='Profile';
public $belongsTo='User';
}
以下是控制器中的add()函数:
public function add() {
$this->set('title_for_layout', 'Add Profiles');
if ($this->request->is('post')) {
$this->Profile->create();
if ($this->Profile->save($this->request->data)) {
$this->Session->setFlash(__('You added a user.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add user.'));
}
}
以下是观点:
<?php
echo $this->Form->create('Profile');
echo $this->Form->input('firstname');
echo $this->Form->input('lastname');
echo $this->Form->input('dob', array(
'dateFormat'=>'DMY',
'minYear' => date('Y')- 1900,
'maxYear' => date('Y')- 2013));
echo $this->Form->end('Save Profile');
?>
你能发布你的数据库模式吗?您使用什么作为主键
更新:查看架构后,CakePHP不支持多列主键。请使用单个主键,如果需要,请为第二个键编制索引。可以发布数据库架构吗?您使用什么作为主键
更新:查看架构后,CakePHP不支持多列主键。改为使用单个主键,如果需要,请索引第二个键。添加了架构。主键是“id”和“user\u id”。更新了我的答案,请参阅上面的“user\u id”主键和索引的“id”。还是不行。你不能这样做,读懂并使主键为
id
。然后它就可以工作了。可以在模型中将主键定义为用户id。但是,您仍然只能使用一列作为添加到架构中的主键。主键是“id”和“user\u id”。更新了我的答案,请参阅上面的“user\u id”主键和索引的“id”。还是不行。你不能这样做,读懂并使主键为id
。然后它就可以工作了。可以在模型中将主键定义为用户id。但是,您仍然只能使用一列作为主键
CREATE TABLE `profiles` (
`id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`firstname` text NOT NULL,
`lastname` text NOT NULL,
`dob` date NOT NULL,
`pob` varchar(100) NOT NULL,
`greatest_acc` varchar(500) NOT NULL,
`fav_food` varchar(200) NOT NULL,
PRIMARY KEY (`id`,`user_id`),
UNIQUE KEY `id` (`id`,`user_id`),
KEY `userid` (`user_id`),
KEY `userid_2` (`user_id`),
KEY `user_id` (`user_id`),
KEY `user_id_2` (`user_id`),
KEY `user_id_3` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;