在 cakephp3 中递归?

Recursive in cakephp3?

这是我的 table 关联代码:

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 table 数据.. 那么我怎样才能获得关联的 tables 数据呢??

使用 contain()

从手册中复制粘贴:

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 在进入试错驱动开发之前! 如果您在此之前已经完成了手册中的教程之一,那么您会很清楚。所以现在就做它们,它们将涵盖更多基础知识。