简体   繁体   English

CakePHP:在不同的控制器中使用模型

[英]CakePHP: using models in different controllers

I have a controller/model for projects. 我有一个项目的控制器/模型。 so this controls the projects model, etc, etc. I have a homepage which is being controlled by the pages_controller. 所以这控制了项目模型等等。我有一个由pages_controller控制的主页。 I want to show a list of projects on the homepage. 我想在主页上显示项目列表。 Is it as easy as doing: 这样做很简单:

function index() {
    $this->set('projects', $this->Project->find('all'));        
}

I'm guessing not as I'm getting: 我猜不是因为我得到了:

Undefined property: PagesController::$Project

Can someone steer me in the right direction please, 有人可以指引我朝正确的方向,

Jonesy Jonesy

You must load every model in the controller class by variable $uses , for example: 您必须通过变量$uses加载控制器类中的每个模型,例如:

var $uses = array('Project');

or in action use method 或在行动中使用方法

$this->loadModel('Project');

In my opinion the proper way to do this is add a function to your current model which instantiates the other model and returns the needed data. 在我看来,正确的方法是在当前模型中添加一个函数,该函数实例化另一个模型并返回所需的数据。

Here's an example which returns data from the Project model in a model called Example and calls the data in the Example controller: 下面是一个示例,它在名为Example的模型中从Project模型返回数据,并在Example控制器中调用数据:

Using Project Model inside Example Model: 使用示例模型中的项目模型:

<?php
/* Example Model */
App::uses('Project', 'Model');
class Example extends AppModel {

    public function allProjects() {
        $projectModel = new Project();
        $projects = $projectModel->find('all');
        return $projects;
    }

}

Returning that data in Example Controller 在示例控制器中返回该数据

// once inside your correct view function just do:
$projects = $this->Example->allProjects();
$this->set('projects', $projects);

In the Example view 在示例视图中

<?php
// Now assuming you're in the .ctp template associated with
// your view function which used: $projects = $this->Example->allProjects();
// you should be able to access the var: $projects
// For example:
print_r($projects['Project']);

Why is this "better" practice than loading both models into your controller? 为什么这种“更好”的做法比将两种模型加载到控制器中? Well, the Project model is inherited by the Example model, so Project data now becomes part of the Example model scope. 好吧,Project模型由Example模型继承,因此Project数据现在成为Example模型范围的一部分。 (What this means on the database side of things is the 2 tables are joined using SQL JOIN clauses). (这意味着在数据库方面的事情是使用SQL JOIN子句连接2个表)。

Or as the manual says: 或者如手册所说:

One of the most powerful features of CakePHP is the ability to link relational mapping provided by the model. CakePHP最强大的功能之一是能够链接模型提供的关系映射。 In CakePHP, the links between models are handled through associations. 在CakePHP中,模型之间的链接通过关联来处理。 Defining relations between different objects in your application should be a natural process. 在应用程序中定义不同对象之间的关系应该是一个自然的过程。 For example: in a recipe database, a recipe may have many reviews, reviews have a single author, and authors may have many recipes. 例如:在食谱数据库中,食谱可能有很多评论,评论有一个作者,作者可能有很多食谱。 Defining the way these relations work allows you to access your data in an intuitive and powerful way. 定义这些关系的工作方式允许您以直观和强大的方式访问数据。 ( source ) 来源

For me it's more reasonable to use requestAction. 对我来说,使用requestAction更合理。 This way the logic is wrapped in the controller. 这样逻辑就包含在控制器中。

In example: 例如:

//in your controller Projects: //在您的控制器项目中:

class ProjectsController extends AppController {
  function dashboard(){
    $this->set('projects', $this->Project->find('all'));
  }
  $this->render('dashboard');
}

Bear in mind that you need to create dashboard.ctp in /app/views/projects of course. 请记住,您需要在/ app / views / projects中创建dashboard.ctp。

In the Page's dashboard view (probably /app/views/pages/dashboard.ctp) add: 在Page的仪表板视图(可能是/app/views/pages/dashboard.ctp)中添加:

echo $this->requestAction(array('controller'=>'projects', 'action'=>'dashboard'));

This way the logic will remain in the project's controller. 这样逻辑将保留在项目的控制器中。 Of course you can request /projects/index, but the handling of the pagination will be more complicated. 当然你可以请求/ projects / index,但是分页的处理会更复杂。

more about requestAction() . 更多关于requestAction()的信息 but bear in mind that you need to use it carefully. 但请记住,你需要仔细使用它。 It could slow down your application. 它可能会减慢您的应用程序。

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

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