私は CakePHP を初めて使用します。create 関数を使用して新しい行を追加すると、次のエラーが発生します。
整合性制約違反: 1062 キー 'PRIMARY' の重複エントリ '0-0'
Formhelper を使用して新しいデータを保存するたびに、Profile.id が 0 に設定されますが、その ID を持つ行が既に存在します。User.id と profile.id の両方が自動インクリメントに設定されています。
私の質問は次のとおりです: 次の使用可能な ID を使用してデータを保存するにはどうすればよいですか?
モデルは次のとおりです。
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');
?>
----編集 20 10 月 13 日----
データベース スキーマ:
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;