CakePHP 3: Save Associated Model Fail

∥☆過路亽.° 提交于 2019-12-08 10:38:07

问题


I'm having difficulty using Cakephp 3 patchEntity to save associated models. The models involved are summarized here

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'
    ]);

}

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']] );

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
        )

)

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?


回答1:


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.

The option should look exactly the same as in the save() call, ie

$user = $this->patchEntity($user, $this->request->data, [
    'associated' => ['UsersExtraTemp']
]);

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
        )

)

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,
        // ...
    ];

    // ...
}

See also

  • 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


来源:https://stackoverflow.com/questions/31662069/cakephp-3-save-associated-model-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!