模型在MVC中的作用是什么以及如何使其工作?

时间:2014-12-26 17:43:47

标签: php model-view-controller

我现在开始理解模型 - 视图 - 控制器模式,除了我认为我被卡住的一件事,模式的模型部分。

我现在已经通过简单地路由请求,获取参数并将它们解析为扩展核心功能并生成视图的适当控制器,成功地使Controller和View工作。

现在我真的不知道如何使模型工作。据我所知,模型只是数据的结构,所以这里我有一个名为Article.php的文件,它是博客内容的模型

Article.php

<?php
class Article extends Database {
    public $id;
    public $topic;
    public $content;
}
?>

此类扩展了Database类,如下所示

database.php中

<?php
    class Database {

    // Current connection which only one connection is allowed in this version
    protected $connection;

    // MySql Setting Array
    protected $dbsettings = array();

    public function __construct() {
        require_once 'bin/config/db.php';
        foreach($db_setting_array as $k => $v) {
            $this->dbsettings[$k] = $v;
        }

        $this->connection = new mysqli(
            $this->dbsettings['server'],
            $this->dbsettings['username'],
            $this->dbsettings['password'],
            $this->dbsettings['database']
        );
    }

    public function inject($qr) {
        $result = $this->connection->query($qr);
    }

    public function call($qr) {
        $result = $this->connection->query($qr);
        return $result;
    }
}
?>

并且article类由来自blog.php的buildData方法加载,其中Blog类扩展了核心控制器

blog.php的

<?php
 class blog extends Controller {

    protected $article;

    public function __construct() {
        /* Load data structure of Entry.php */
        $this->article = $this->BuildData('Entry');
    }

    public function all($linkname='LINK BACK HOME') {
        $this->loadtemplate('blog/blog', array('linkname'=>$linkname));
    }

    public function each($blogid = '') {
        // Query the database here
        $result = $this->article->call('select * from articles where id='.$blogid);
        while($rows = $result->fetch_assoc()) {
            $data['id'] = $rows['id'];
            $data['title'] = $rows['title'];
            $data['content'] = $rows['content'];
        }
    $this->loadtemplate('blog/each', $data);
       $query = 'insert into articles(title, content) values (\'test2\', \'test2\')';
       $this->article->inject($query);
    }
};
?>

你可以看到在Database.php中我声明了两个用于查询数据库的注入和调用方法,我在Blog.php中使用它来扩展核心控制器。

使用所有这些代码,我不需要对模型做任何事情。我可以使用我声明的“inject”和“call”方法查询数据库,并将结果数组解析为视图,如Blog.php中所示。

那么Model如何真正起作用,如果我想使用它呢?或者如何更新,控制器或Model类本身?

1 个答案:

答案 0 :(得分:0)

模型应该与Database类交互,因为在MVC模型中Model负责获取数据,处理数据并传递给视图。在您的情况下,博客类(源自Controller)不遵循MVC模型。相反,它应该实例化一篇文章&#34;类实例并将控件传递给&#34;文章&#34;用于从数据库中获取数据的类。

article.php应该是

<?php
class Article extends Database {
    public $id;
    public $topic;
    public $content;
    public $database; //Dependency injection

    //This method has been moved from the blog class
    public function each(&$data, $blogid = '') {
        // Query the database here
        $result = $this->database->call('select * from articles where id='.$blogid);
        while($rows = $result->fetch_assoc()) {
            $data['id'] = $rows['id'];
            $data['title'] = $rows['title'];
            $data['content'] = $rows['content'];
        }
    }

    public function inject() {
        $query = 'insert into articles(title, content) values (\'test2\', \'test2\')';
        $this->database->inject($query);
    }
}
?>

并且blog.php应为

<?php
class Blog extends Controller {

    protected $article;

    public function __construct() {
        /* Load data structure of Entry.php */
        $this->article = $this->BuildData('Entry');
    }

    public function all($linkname='LINK BACK HOME') {
        $this->loadtemplate('blog/blog', array('linkname'=>$linkname));
    }

    public function getData($blogid = '') {
       $data = array();
       $this->article->each($data, $blogid);
       $this->loadtemplate('blog/each', $data);
       $this->article->inject();
    }
}
?>

注意事项:

  1. 模型仅与数据交互。将来,如果您更改数据存储区(从数据库迁移到云),则只需更改模型。
  2. 控制器具有基于数据构建视图的所有业务逻辑。当您的应用程序变得复杂时,控制器将会改变这种设计原则被称为&#34;关注点分离&#34;控制器和模型现在负责完全独立的功能。