cakephp3无法使用“关联”方法在关联表中插入数据 [英] cakephp3 unable to insert data in associated tables using 'associated' method

查看:285
本文介绍了cakephp3无法使用“关联”方法在关联表中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个表:UserMasters并与PersonMasters关联:

  $ this-> hasOne('person_masters' ,[
'className'=>'person_masters',
'foreign_key'=>'user_master_id',
'dependent'=> true
]

但是当我保存数据时,它只保存user_masters table.i中的数据,方法在patchEntity。但它不工作。这是我的保存数据的代码:

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

它只保存数据到user_masters ..so我怎么也可以保存数据在person_masters在cakephp3?

如果 person_masters


_accessible属性允许您提供属性的地图,以及是否不是他们可以批量分配。值true和false表示字段是否可以批量分配。


http://book.cakephp.org/3.0/en/orm/entities.html#mass-assignment



也使用表别名定义关联。

  $ this-> hasOne('PersonMasters',[
'className'=>'PersonMasters',
'foreign_key'=>'user_master_id',
'dependent'=> true
]);


I am using one table:UserMasters and associate with PersonMasters using:

 $this->hasOne('person_masters', [
            'className' => 'person_masters',
            'foreign_key'=>'user_master_id',
            'dependent' => true
        ]);

But when i am saving data it only save data in user_masters table.i am also using 'associated' method in patchEntity. but it not worked..Here is my code for save data:

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

It only save data to user_masters ..so how can i also save data in person_masters in cakephp3?? thanks in advance...

解决方案

If person_masters isn't accessible, it won't be assigned like this.

The _accessible property allows you to provide a map of properties and whether or not they can be mass-assigned. The values true and false indicate whether a field can or cannot be mass-assigned.

http://book.cakephp.org/3.0/en/orm/entities.html#mass-assignment

also use table alias for defining associations.

$this->hasOne('PersonMasters', [
        'className' => 'PersonMasters',
        'foreign_key'=>'user_master_id',
        'dependent' => true
    ]);

这篇关于cakephp3无法使用“关联”方法在关联表中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆