CakePHP有一个关系,多个储蓄
我需要一些关于hasOne关系的帮助,该关系使用同一外键保存多个记录。这种关系是:CakePHP有一个关系,多个储蓄,cakephp,Cakephp,我需要一些关于hasOne关系的帮助,该关系使用同一外键保存多个记录。这种关系是: PersonalbelongsToUserAccount UserAccounthasOnePersonal 在名为personals的表中,我将外键放入了user\u accounts,它被称为user\u account\u id,只有在我的数据库中手动选择此外键作为唯一外键时,限制才起作用 我认为CakePHP可以处理这种情况。或者我错过了什么 谢谢你的帮助 编辑 在我的add表单中,我有一行echo$th
Personal
belongsToUserAccount
UserAccount
hasOnePersonal
在名为personals
的表中,我将外键放入了user\u accounts
,它被称为user\u account\u id
,只有在我的数据库中手动选择此外键作为唯一外键时,限制才起作用
我认为CakePHP可以处理这种情况。或者我错过了什么
谢谢你的帮助
编辑
在我的add表单中,我有一行echo$this->form->input('UserAccount.id',数组('type'=>'hidden','value'=>$UserAccount\u id))
其中,$useraccount\u id
取自我的useraccounts控制器
:
public function new_personal() {
$current_user = $this->Auth->user('id');
$current_account = $this->UserAccount->find('first', array(
'conditions' => array('UserAccount.user_id' => $current_user)));
$this->set('useraccount_id', $current_account['UserAccount']['id']);
if ($this->request->is('post')) {
if (!empty($this->request->data)) {
unset($this->UserAccount->Personal->validate['user_account_id']);
$this->UserAccount->saveAssociated($this->request->data);
$this->Session->setFlash(__('Ok'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Error'));
}
}
我还尝试将
unique=>true
属性添加到hasOne和belongsTo关系(在我的模型上),但没有成功。我找到的唯一方法是直接在数据库中设置唯一键。编辑过程中必须设置个人行的ID。
另外,在您的表中设置唯一键od字段用户\帐户\ id你好,kicaj,谢谢您的回答。请检查我的编辑。你怎么认为?