gpt4 book ai didi

mysql - cakephp3 中的递归?

转载 作者:行者123 更新时间:2023-11-29 07:29:59 24 4
gpt4 key购买 nike

这是我的表关联代码:

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

当我在 Controller 中使用时:$this->UserMasters->get($id);它只产生 user_masters 表数据..那么我怎样才能获取关联表数据?

最佳答案

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

关于mysql - cakephp3 中的递归?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33015910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com