gpt4 book ai didi

mysql - 访问 cakephp3 Controller 中的非关联表

转载 作者:行者123 更新时间:2023-11-30 21:43:30 25 4
gpt4 key购买 nike

我是 cakephp3 的新手,我正在尝试访问表 2 Controller 中未链接到“表 B”的“表 A”

我想在 UsersController 中使用用户类别表。如何实现?

例如

user table has three fields id, name, role
category table has 2 fields id, name
articles table has 3 fields id, user_id, category_id, article

最佳答案

在你的 Controller 中你可以使用loadModel()加载另一个未链接到 Controller 默认模型的表。

class UsersController extends AppController {

public function initialize() {
parent::initialize();
// You don't have to put it in initiliaze
// but if you want to use it in more than one method it's a good place
$this->loadModel('Categories');
$this->loadModel('Articles');
}

public function index() {
// Categories is now available as $this->Categories thanks to loadModel()
$categories = $this->Categories->find()->select(['id', 'name']);
}

}

关于mysql - 访问 cakephp3 Controller 中的非关联表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50552394/

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