保存cakephp 3中的关联项不工作
我在cakephp3(3.1.5和3.0)中关联的保存不起作用保存cakephp 3中的关联项不工作,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我在cakephp3(3.1.5和3.0)中关联的保存不起作用 用户与用户配置文件的表关系表是一个,而用户配置文件与用户的表关系表是一个 当我想将用户记录与关联的用户配置文件记录一起保存时,用户记录保存在users表中,但关联的表user\u profiles中不保存任何记录。我在error.log中没有错误 实体 对于他们两个=> protected $_accessible = [ * => true ]; 用户控制器 您的'associated'=>['UserProfil
用户
与用户配置文件的表关系
表是一个,而用户配置文件
与用户的表关系
表是一个
当我想将用户记录与关联的用户配置文件记录一起保存时,用户记录保存在users
表中,但关联的表user\u profiles
中不保存任何记录。我在error.log中没有错误
实体 对于他们两个=>
protected $_accessible = [
* => true
];
用户控制器
您的
'associated'=>['UserProfiles']
位于错误的位置。将他添加到newEntity()
或patchEntity()
编辑原始问题并用适当的信息填充,而不是创建重复的问题。默认情况下,第一级关联正在合并,只有在更深层次或禁用/限制关联合并时才需要使用associated。我也面临同样的问题。以上回复无效,请帮我解决。
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.'));
}
}
...
$entity = $this->Users->patchEntity($entity, $data, [
'associated' => ['UserProfiles']
]);
if ($this->Users->save($entity))
...