this is my associations at UsersTable
users has one personal info
$this->addAssociations([
'hasOne' => [
'Personal_info'=>[
'dependent' => true,
'cascadeCallbacks' => true,
'foreignKey' => 'user_id',
],
'status'=>[
'dependent' => true,
'cascadeCallbacks' => true,
]
]
]);
this is my personalInfoTable
belongsTo Users
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER',
]);
my edit action in controller
public function edit()
{
$uuid = $this->Auth->User('uid');
$edit = $this->Users->get($uuid,['contain'=> 'personal_info']);
if ($this->request->is(['post','put']))
{
$this->Users->patchEntity($edit, $this->request->getData(),[
'associated' => [
'personal_info'
]
]);
if ($this->Users->save($edit))
{
debug($edit);
$this->Flash->success('account edited!');
}else{
$this->Flash->error('not edited!');
debug($edit->getErrors());
debug($edit);
}
}
}
and then my view temp
<?php
/**
* @var \App\View\AppView $this
* @var mixed $user
*/
?>
<?php
echo $this->Form->create();
echo $this->Form->control('username');
echo $this->Form->control('password');
echo $this->Form->control('email');
echo $this->Form->control('phone');
echo $this->Form->hidden('personal_info.pid');
echo $this->Form->control('personal_info.first_name');
echo $this->Form->control('personal_info.middle_name');
echo $this->Form->control('personal_info.last_name');
echo $this->Form->control('personal_info.sex');
echo $this->Form->control('personal_info.age');
echo $this->Form->button('edit');
echo $this->Form->end();
my result is it do not update my association entity it creates new entity with the same foriegn key as the editable fiield
\src\Controller\UsersController.php (line 252)
object(App\Model\Entity\User) {
'uid' => (int) 45,
'username' => 'ermiasss',
'password' => '$2y$10$py.LVQHWZRFZBq1nUzN5.u7c6OThaPKCbETWt//DTz.OVbxZ0gAZO',
'email' => 'ermiassa@gmail.com',
'phone' => '0961537212',
'role' => '3',
'created' => object(Cake\I18n\FrozenTime) {
'time' => '2021-12-22 08:32:05.000000+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\FrozenTime) {
'time' => '2021-12-26 17:26:18.027769+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'Personal_info' => [
'pid' => (int) 22,
'first_name' => 'hermela',
'middle_name' => 'mesfin',
'last_name' => 'abebe',
'sex' => (int) 2,
'age' => (int) 15,
'user_id' => (int) 45,
'created' => object(Cake\I18n\FrozenTime) {
'time' => '2021-12-24 03:27:00.000000+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\FrozenTime) {
'time' => '2021-12-24 03:27:00.000000+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
}
],
'personal_info' => object(App\Model\Entity\PersonalInfo) {
'first_name' => 'ermias',
'middle_name' => 'mesfin',
'last_name' => 'abebe',
'sex' => (int) 1,
'age' => (int) 24,
'user_id' => (int) 45,
'created' => object(Cake\I18n\FrozenTime) {
'time' => '2021-12-26 17:26:18.172551+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\FrozenTime) {
'time' => '2021-12-26 17:26:18.172686+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'pid' => (int) 31,
'[new]' => false,
'[accessible]' => [
'first_name' => true,
'middle_name' => true,
'last_name' => true,
'sex' => true,
'age' => true,
'user_id' => true,
'created' => true,
'modified' => true,
'user' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Personal_info'
},
'[new]' => false,
'[accessible]' => [
'username' => true,
'password' => true,
'email' => true,
'phone' => true,
'role' => true,
'created' => true,
'modified' => true,
'personal_info' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[hasErrors]' => false,
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Users'
}
the users table is good updating but the associate table personal_info is create new with same foriegn key