访问cakephp3控制器中的非关联表

slsn1g29  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(392)

我是cakephp3的新手,我正在尝试访问一个“表a”,它没有链接到表2中控制器的“表b”
我想在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
swvgeqrz

swvgeqrz1#

在控制器中,可以使用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']);
    }

}

相关问题