php CakePHP:在不同的控制器中使用模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3696701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP: using models in different controllers
提问by iamjonesy
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. I want to show a list of projects on the homepage. Is it as easy as doing:
我有一个用于项目的控制器/模型。所以这控制了项目模型等。我有一个由 pages_controller 控制的主页。我想在主页上显示项目列表。是不是像做一样简单:
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
琼斯
回答by Tomasz Kowalczyk
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');
回答by DrewT
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. (What this means on the database side of things is the 2 tables are joined using SQL JOIN
clauses).
为什么这种“更好”的做法比将两个模型加载到您的控制器中要好?好吧,Project 模型由 Example 模型继承,因此 Project 数据现在成为 Example 模型范围的一部分。(这在数据库方面意味着两个表是使用 SQLJOIN
子句连接的)。
Or as the manual says:
或者如手册所说:
One of the most powerful features of CakePHP is the ability to link relational mapping provided by the model. In CakePHP, the links between models are handled through associations. 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)
CakePHP 最强大的特性之一是能够链接模型提供的关系映射。在 CakePHP 中,模型之间的链接是通过关联处理的。定义应用程序中不同对象之间的关系应该是一个自然的过程。例如:在一个菜谱数据库中,一个菜谱可能有很多评论,评论只有一个作者,作者可能有很多菜谱。定义这些关系的工作方式可以让您以一种直观而强大的方式访问您的数据。(来源)
回答by Nik Chankov
For me it's more reasonable to use requestAction. This way the logic is wrapped in the controller.
对我来说使用 requestAction 更合理。通过这种方式,逻辑被包装在控制器中。
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:
在页面的仪表板视图(可能是 /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(). but bear in mind that you need to use it carefully. It could slow down your application.
更多关于requestAction()。但请记住,您需要谨慎使用它。它可能会减慢您的应用程序。