在cakephp3中递归吗? [英] Recursive in cakephp3?

查看:112
本文介绍了在cakephp3中递归吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的表关联代码:

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

当我在控制器中使用时: $ this-> UserMasters-> get($ id); 它仅产生user_masters表数据. 所以我怎么也可以得到关联表的数据呢?

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??

推荐答案

使用contain()

从手册中复制粘贴:

Use contain()

Copy-Paste from the manual:

要加载主要模型时,应使用contain(),并且 它的相关数据.虽然contain()可以让您应用其他 加载的关联的条件,您不能约束 基于关联的主要模型.有关的更多详细信息 contains(),查看热切加载关联".

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.

这篇关于在cakephp3中递归吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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