我需要一些帮助的hasOne关系,是保存多个记录使用相同的外键。这种关系是:
Personal belongsTo UserAccount
UserAccount hasOne Personal
在名为personals的表中,我将外键放入user_accounts,称为user_account_id,只有在手动选择此外键作为唯一的数据库时,限制才有效。
我认为CakePHP可以处理这种情况。还是我漏掉了什么?
谢谢你的帮忙!
编辑
在我的add表单中,我有行echo $this->Form->input('UserAccount.id', array('type' => 'hidden', 'value' => $useraccount_id));,其中$useraccount_id是从我的UserAccountsController中提取的
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关系中(在我的模型上),但是它没有工作。我找到的唯一方法是直接在我的数据库中设置唯一的键。
发布于 2014-03-18 08:03:26
必须设置个人行持续编辑的ID。附加设置表中的唯一键d字段user_account_id
https://stackoverflow.com/questions/22464963
复制相似问题