[英]Query from model to Controller
I'm learning the whole MVC type of programming. 我正在学习整个MVC类型的编程。 I have a MySQl Query inside a model like so: 我在这样的模型中有一个MySQl查询:
<?php
class BlogModel extends BaseModel {
public static function dbBlog() {
$db = new Database(true);
$entries = $db->matrix('cms_m1_blog_ml');
$this->set('data_blog', $entries);
}
}
This script worked when I had it in the controller. 当我将其放在控制器中时,此脚本有效。 the view looks like this: 该视图如下所示:
<?php
foreach($data_blog as $blog)
{
echo "<ul>";
echo "<li class='name'>Name:".$blog['title'] . "</li>";
echo "<li class='content'>Content:".$blog['content'] . "</li>";
echo "</ul>";
}
How do I pass the data from the model to the controller? 如何将数据从模型传递到控制器? I ave tried all sorts of things but nothing was in the right direction. 我尝试过各种方法,但是没有正确的方向。 I use a custom framework build by my employee but it is based on CakePHP. 我使用员工自定义的框架构建,但是它基于CakePHP。
使用这样的东西
$this->set('data_blog', BlogModel::dbBlog());
Give the controller a reference to the model. 给控制器一个参考模型。 If the model is a static class, simply add a static variable to the controller class, else pass the correct model object as a parameter to the class constructor and store it in a property of the object. 如果模型是静态类,则只需向控制器类添加静态变量,否则将正确的模型对象作为参数传递给类构造函数,并将其存储在对象的属性中。 Like this: 像这样:
class BlogModel extends BaseModel {
private $db;
private $entries;
public static function dbBlog() {
$db = new Database(true);
$entries = $db->matrix('cms_m1_blog_ml');
}
// Add getters here
}
class BlogController {
// Get mymodel somehow and store it
public function doStuff() {
$this->mymodel.getDB();
}
}
Found the anwser :) 找到了anwser :)
Controller: 控制器:
public function blog(){
$this->set('data_blog', BlogModel::dbBlog());
}
public function index() {
}
}
Model: 模型:
public static function dbBlog() {
$db = new Database(true);
$entries = $db->matrix('cms_m1_blog_ml');
return $entries;
}
}
So the problem was in the model, I just needed to return the entries and use the set in the controller. 因此问题出在模型中,我只需要返回条目并在控制器中使用集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.