CakePHP 3:保存关联模型失败 [英] CakePHP 3: Save Associated Model Fail
问题描述
我在使用Cakephp 3 patchEntity保存关联模型时遇到困难。此处汇总了所涉及的模型
I'm having difficulty using Cakephp 3 patchEntity to save associated models. The models involved are summarized here
我的UsersTempTable
My UsersTempTable
public function initialize(array $config)
{
$this->table('users_temp');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasOne( 'UsersExtraTemp', [
'foreignKey' => 'user_id'
]);
}
然后我的UsersExtraTempTable
Then my UsersExtraTempTable
public function initialize(array $config)
{
$this->table('users_extra_temp');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('UsersTemp', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['user_id'], 'UsersTemp'));
return $rules;
}
Mi function to save the data:
$user = $this->newEntity();
$user = $this->patchEntity($user, $this->request->data, [
'associated' => ['UsersTemp.UsersExtraTemp']
]);
$this->save( $user, ['associated' => ['UsersExtraTemp']] );
我的数据数组由$ this-> debug()
And my array of data print by $this->debug()
(
[name] => name
[lastname] => lastname
[email] => email@email.com
[password] => password
[passwordConfirm] => repeatPassord
[UsersExtraTemp] => Array
(
[google_token] => sjskdasdadk2
)
)
我得到一个在数据库中为user_temp创建的行,但对于我期望的一个users_extra没有任何作用。知道我在做什么错吗?
I get a row created for user_temp in the database but nothing for the one users_extra that I'm expecting. Any idea what I'm doing wrong please?
推荐答案
给出 $ this
引用 UsersTempTable
, patchEntity()$ c $的
associated
选项c>不应包含该名称,因为这表明 UsersTempTable
与 UsersTempTable
相关联,情况并非如此
Given that $this
refers to UsersTempTable
, the associated
option for patchEntity()
should not contain that name, as this would suggest that UsersTempTable
is associated with UsersTempTable
, which is not the case.
该选项应与 save()
调用中的选项完全相同,即
The option should look exactly the same as in the save()
call, ie
$user = $this->patchEntity($user, $this->request->data, [
'associated' => ['UsersExtraTemp']
]);
此外,在数据中,您还应该为关联使用正确的属性名称,如果出现 hasOne
关联,是关联名称的单数形式,即强调的变体,即 users_extra_temp
Also in the data you should use the proper property name for the association, which, in case of a hasOne
association, is the singular, underscored variant of the association name, ie users_extra_temp
(
// ...
[users_extra_temp] => Array
(
[google_token] => sjskdasdadk2
)
)
最后但并非最不重要的一点,请确保在 UsersTemp
实体 $ _ accessible
属性
And last but not least, make sure that the property name is defined in the UsersTemp
entities $_accessible
property
class UsersTemp extends Entity
{
// ...
protected $_accessible = [
// ...
'users_extra_temp' => true,
// ...
];
// ...
}
另请参见
- 菜谱>数据库访问和ORM>保存数据>保存HasOne关联
- 菜谱>数据库访问& ORM>保存数据>更改可访问字段
- 菜谱>数据库访问和ORM>实体>批量分配
- 菜谱>数据库访问& ORM>关联> HasOne关联
- Cookbook > Database Access & ORM > Saving Data > Saving HasOne Associations
- Cookbook > Database Access & ORM > Saving Data > Changing Accessible Fields
- Cookbook > Database Access & ORM > Entities > Mass Assignment
- Cookbook > Database Access & ORM > Associations > HasOne Associations
这篇关于CakePHP 3:保存关联模型失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!