保存在cakephp 3中关联的文件不起作用 [英] Save associated in cakephp 3 not working
问题描述
我在cakephp3(3.1.5和3.0)中关联的保存不起作用。
My save associated in cakephp3 (3.1.5 and 3.0) is not working.
users
表关系与 user_profiles
表是hasOne和 user_profiles
表关系与 users
users
table relation withuser_profiles
table is hasOne and user_profiles
table relation with users
table is belongsTo.
当我想保存带有相关user_profile记录的用户记录时,该用户记录保存在个用户
中表,但没有记录保存在关联的表 user_profiles
中。我没有error.log
When I want to save a user record with associated user_profile record, user record saves in users
table but no record is saved in associated table user_profiles
. I have no error in error.log
对于他们两个=>
protected $_accessible = [
* => true
];
用户控制器
Users Controller
public function add()
{
$data = [
'email' => 'example@test.com',
'username' => 'Example',
'password' => '123456789',
'user_profile' => [
'first_name' => 'John',
'last_name' => 'Smit',
'phone' => '123456798'
]
];
$entity = $this->Users->newEntity();
$entity = $this->Users->patchEntity($entity, $data);
if ($this->Users->save($entity, [
'associated' => ['UserProfiles']
])) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
推荐答案
您的'associated'=> ['UserProfiles']
在错误的位置。将他添加到 newEntity()
或 patchEntity()
;
Your 'associated' => ['UserProfiles']
is on wrong place. add him to newEntity()
or patchEntity()
;
...
$entity = $this->Users->patchEntity($entity, $data, [
'associated' => ['UserProfiles']
]);
if ($this->Users->save($entity))
...
这篇关于保存在cakephp 3中关联的文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!