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


What is the role of model in MVC and how to make it work?

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

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

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

文章.php

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

此类扩展了数据库类,如下所示

数据库.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;
    }
}
?>

文章类由 buildData 方法从博客加载.php具有扩展核心控制器的博客类

博客.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);
    }
};
?>

您可以看到,在数据库中.php我声明了两个方法,分别是 inject 和 call,用于查询数据库,我在博客中使用它.php它扩展了核心控制器。

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

那么模型是如何工作的,如果我想使用它呢?或者应该如何在控制器或模型类本身内部更新它?

模型应该与数据库类交互,因为在MVC模型中,模型负责获取数据,处理数据并传递给视图。在您的情况下,博客类(派生自控制器)不遵循 MVC 模型。相反,它应该实例化一个"article"类实例,并将控件传递给"article"类以从数据库中获取数据。

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. 控制器具有基于数据构造视图的所有业务逻辑。当您的应用程序变得复杂时,控制器将发生变化。这种设计原则称为"关注点分离",其中控制器和模型现在负责完全独立的功能。