Recursive in cakephp3?

六月ゝ 毕业季﹏ 提交于 2019-12-13 06:00:49

问题


here is my table association code:

class UserMastersTable extends Table {
  public function initialize(array $config) {
        parent::initialize($config); 
        $this->table('user_masters');     
        $this->hasOne('person_masters', [
            'className' => 'person_masters',
            'foreign_key'=>'user_master_id',
            'dependent' => true
        ]);
    }
}

when in controller i am using: $this->UserMasters->get($id); it results only user_masters table data.. so how can i also get Associated tables data??


回答1:


Use contain()

Copy-Paste from the manual:

You should use contain() when you want to load the primary model, and its associated data. While contain() will let you apply additional conditions to the loaded associations, you cannot constrain the primary model based on the associations. For more details on the contain(), look at Eager Loading Associations.

// In a controller or table method.

// As an option to find()
$query = $articles->find('all', ['contain' => ['Authors', 'Comments']]);

// As a method on the query object
$query = $articles->find('all');
$query->contain(['Authors', 'Comments']);

Read the manual before jumping into trial and error driven development! If you would have done one of the tutorials in the manual before this would be clear. So do them now, they'll cover a lot more of the basics.



来源:https://stackoverflow.com/questions/33015910/recursive-in-cakephp3

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