I need some help with a hasOne relationship that is saving multiple records with the same foreign key. The relationship is:
Personal
belongsTo UserAccount
UserAccount
hasOne Personal
In the table named personals
I've put the foreign key to user_accounts
, which is called user_account_id
, and the restriction only works if I manually select in my database this foreign key as Unique.
I thought CakePHP can manage this situations. Or maybe I'm missing something?
Thanks for your help!
EDIT
In my add form I have the line echo $this->Form->input('UserAccount.id', array('type' => 'hidden', 'value' => $useraccount_id));
where $useraccount_id
is taken from my 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'));
}
}
I've also tried to add the unique => true
property to the hasOne and belongsTo relationships (on my models) but it didn't work. The only way I find is to set the Unique key directly in my database.