【发布时间】:2015-07-27 19:54:01
【问题描述】:
我在使用 Cakephp 3 patchEntity 保存关联模型时遇到了困难。这里总结了涉及的模型
我的用户临时表
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
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函数保存数据:
$user = $this->newEntity();
$user = $this->patchEntity($user, $this->request->data, [
'associated' => ['UsersTemp.UsersExtraTemp']
]);
$this->save( $user, ['associated' => ['UsersExtraTemp']] );
我的数据数组由 $this->debug() 打印
(
[name] => name
[lastname] => lastname
[email] => email@email.com
[password] => password
[passwordConfirm] => repeatPassord
[UsersExtraTemp] => Array
(
[google_token] => sjskdasdadk2
)
)
我在数据库中为 user_temp 创建了一行,但没有为我所期待的 users_extra 创建一行。请知道我做错了什么吗?
【问题讨论】:
-
那么,你保存过程中的
$this是UsersTempTable? -
是的@ndm .. 保存的过程是模型本身而不是控制器
标签: php cakephp cakephp-3.0