mysql - 访问 cakephp3 控制器中的非关联表
问题描述
我是 cakephp3 的新手,我正在尝试访问未链接到表 2 控制器中的“表 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
解决方案
在您的控制器中,您可以使用loadModel()加载另一个未链接到控制器默认模型的表。
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']);
}
}
推荐阅读
- vue.js - 如何构建没有 404 错误的 nuxt+vue 路由器?
- python - 脱离 Nodejs 和 Django 我应该学习什么来构建一个涉及网络爬取(爬虫)的网络项目?检查说明
- google-colaboratory - Google-Colaboratory - 如何刷新 google-drive?
- javascript - 检查嵌套数组中的元素
- javascript - 如何在 Javascript(浏览器)中运行 PHP?
- rust - 使用 Futures 和 Tokio 进行 Rust 并发执行
- sql-server-2014 - 将 SQL Server 2014 升级到 SQL Server 2016
- machine-learning - 随机森林是解决这个问题的最佳方法吗?
- java - Eclipse RCP 和 Tycho
- actionscript-2 - 我如何判断我的数组是否已达到最大大小?[动作脚本 2]