简体   繁体   English

从模型查询到控制器

[英]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.

相关问题 Laravel 5.2将查询从控制器移动到模型 - Laravel 5.2 Moving Query to Model from Controller 来自不同模块中控制器的Phalcon查询模型 - Phalcon query model from controller in different module CakePHP 2.0-2.1,从控制器查询不同的模型 - CakePHP 2.0 - 2.1, Query different model from controller 如何从模型向API控制器Laravel调用结果数据库查询 - How call result Database Query from Model to API Controller Laravel 在Codeigniter中无法从模型到控制器获取查询结果 - Cannot get query result from Model to Controller in Codeigniter 将控制器的外键传递给特定查询的模型cakephp 2.3.9 - Pass foreign key from controller to model for specific query cakephp 2.3.9 优化将参数从控制器传递到模型以进行mysql查询的方式 - optimize way of passing paramters from controller to model for mysql query 从控制器解析模型查询结果和控制器公共变量以使用Codeigniter查看 - Parsing model query results and controller public variable from controller to view with codeigniter 如何在模型中运行查询并在控制器中加载? - How to run a query in model and load in the controller? 如果模型中的查询在 Codeigniter 3 中返回空,则重定向到控制器 - Redirect to controller if query in model return empty in Codeigniter 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM