问题描述
我知道有很多关于MVC和最佳做法的文章和问题,但是我找不到像这样的简单示例:
I know there are plenty of articles and questions about MVC and best practices, but I can't find a simple example like this:
让我们说我必须用PHP开发一个Web应用程序,我想按照MVC模式(没有框架)进行操作.应用程序应具有简单的CRUD书籍.
Lets say I have to develop a web application in PHP, I want to do it following the MVC pattern (without framework).The aplication should have a simple CRUD of books.
我想从控制器中获取商店中所有的书(保存在数据库中).
From the controller I want to get all the books in my store (that are persisted in a database).
模型应该如何?
类似这样的东西:
class Book {
private $title;
private $author;
public function __construct($title, $author)
{
$this->title = $title;
$this->author = $author;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
return this;
}
.
.
.
class BooksService{
public getBooks(){
//get data from database and return it
//by the way, what I return here, should by an array of Books objects?
}
public getOneBook($title){
//get data from database and store it into $the_title, $the_autor
$the_book = new Book($the_title, $the_autor);
return $the_book;
}
.
.
.
所以我这样称呼它(从控制器中):
So I call it(from the controller) like that:
$book_service = new BooksService();
$all_books = $book_service->getBooks();
$one_book = $book_service->getOneBook('title');
或者最好在Books类中包含所有内容,像这样:
Or maybe should be better have everything in the Books class, something like this:
class Book
{
private $title;
private $author;
//I set default arguments in order to create an 'empty book'...
public function __construct($title = null, $author = null)
{
$this->title = $title;
$this->author = $author;
}
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
return this;
}
public getBooks(){
//get data from database and return it
//and hare, what I should return? an Array?
}
public getOneBook($title){
//get data from database and store it into $the_title, $the_autor
$the_book = new Book($the_title, $the_autor);
return $the_book;
}
.
.
.
所以我这样称呼它(从控制器中):
So I call it(from the controller) like that:
$book_obj = new Book();
$all_books = $book_obj->getBooks();
$one_book = $book_obj->getOneBook('title');
或者我完全错了,应该以完全不同的方式进行吗?
Or maybe I'm totally wrong and should by in a very different way?
谢谢!
推荐答案
两种方法都是正确的,并且对应于两种不同的模式.
Both ways are correct and correspond to two different patterns.
您的第一个想法可能与 数据映射器模式 相关联.在这种情况下,您的书本类不必了解任何有关数据库的知识,而让另一个类担心数据库的操作.这是诸如Doctrine2或Hibernate(我认为)之类的项目使用的模式
Your first idea could be associated with the Data mapper pattern.In this case your book class doesn't have to know anything about the database, and let another class worrying about database operations.This is the pattern use by projects like Doctrine2 or Hibernate (I think)
第二个称为 活动记录模式 将所有内容都放在一个类中,您的book对象必须管理自己的持久性.这是Ruby on Rails使用的模式.
The second one is known as Active record pattern where you keep everything in a single class and your book object has to manage its own persistence.This is the pattern used by Ruby on Rails.
第一种方法通常会为您提供更大的灵活性,但是第二种方法可能看起来更直观且易于使用.
The first approach generally give you more flexibility but second one may look more intuitive and easy to use.
一些信息:
http://culttt.com/2014 /06/18/whats-difference-active-record-data-mapper/
这篇关于MVC中的模型(最佳实践,PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!