简体   繁体   English

mysql如何在cakephp中查询不同控制器的不同表

[英]How to mysql query different tables of different controllers in cakephp

I have 4 controllers and one database. 我有4个控制器和一个数据库。 Each controller is associated with one table. 每个控制器与一个表关联。 I am not able to query in one controller if i have to access table of another controller. 我不能在一个控制器中查询是否必须访问另一个控制器的表。 I get "table not found" error. 我收到“找不到表”错误。

You need to define the relations between the tables, using the associations: 您需要使用关联定义表之间的关系:

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

By adding the right colums to your tables (Foreign ID fields) and defining the model associations, you will be able to access all the fields from the related tables. 通过将正确的列添加到表中(外国ID字段)并定义模型关联,您将能够访问相关表中的所有字段。

Your question is a little confusing to say the least. 至少可以说您的问题有点令人困惑。 Are you having issues accessing different models inside different controllers or is this an association issue? 您是否在访问不同控制器内的不同模型时遇到问题,或者这是关联问题? In your controller, you can define what models your controller uses by setting $uses: 在控制器中,可以通过设置$ uses来定义控制器使用的模型:

class UsersController extends AppController {

    $uses = array('User', 'File');

    public function index(){
        $results = $this->File->find('all');
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM