PHP / CodeIgniter中的对象和模型之间有区别吗? [英] Is there a difference between Object and Models in PHP/CodeIgniter?

查看:49
本文介绍了PHP / CodeIgniter中的对象和模型之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过使用CodeIgniter框架来开发图书馆/图书管理系统。我是这方面的新手,拥有自己的学习方式,所以如果这听起来像个愚蠢的问题,请多多包涵。

I've approached the development a library/book management system by using the CodeIgniter framework. I'm new to all this, and have my own way of learning, so bear with me if it sounds like a daft question.

我到了一个对概念和设计有疑问的地步。

I'm getting to a point where I've got questions about concepts and design.

我有很多视图,可以完成大部分工作的控制器和一个模型。我现在意识到,虽然这一切都是不错的MVC,但不一定结构合理。例如,有关一个表/一个模型的参考很多,依此类推。

I've got lot of views, a controller that does most of the work, and one model. I'm now realizing that whilst this is all nicely MVC, it's not necessarily well structured. For example there's lots of reference about one table/one model and so on.

这样我就可以很容易地将模型分为libraries_model和books_model。但是我不确定这是什么:

So I can separate my model into a libraries_model and a books_model easily enough. But what I'm not sure about is this:

1)我的books_model是书籍对象(从概念上来说)吗?还是我应该创建一个明显使用我的 books_model(毕竟是MVC)的单独 book对象。即它们是同一东西吗?

1) Is my books_model the book object (conceptually speaking)? Or should I be creating a separate "book object" which obviously uses my "books_model (this is MVC after all). i.e. Are they one and the same thing?

2 )如果它们是同一回事,那么我只会将我的books_model存储在CodeIgniter文件夹结构的model文件夹中;否则,建议我将book对象(不是模型)放在哪里?

2) If they are the same thing, then I would only have my books_model, stored in the model folder of the CodeIgniter folder structure. If not, then where would I be advised to put the book object (that is not the model)? In the core folder?

我有一种感觉,他们的确是同一回事,但我只是不知道如何确认,除了问那些知道:)

I've got a feeling that they are indeed the same thing, but I just don't know how to confirm it, short of asking those that know :)

推荐答案

我对CodeIgniter所做的一切并不熟悉,但可以这样想:

I'm not familiar with everything CodeIgniter does, but think of it this way:

对于MVC,M很大,很难一概而论,而C很小(V应该很明显)。记住原理:瘦控制器,胖模型( http://www.bing.com/search?q=thin+controllers+fat+models

For MVC, M is big and hard to generalize, whereas C is very small (and the V should be pretty obvious). Remember the principle: Thin Controllers, Fat Models (http://www.bing.com/search?q=thin+controllers+fat+models)

现在,要解决您的问题,这花了我一段时间。大多数框架在文档中谈论的模型并不仅限于模型层。您也可以将其视为服务层。记住,控制器是瘦的,因此它们应该很简单。他们应该做类似的事情,从请求中获取输入,将其传递给其他人以存储在数据库中,获取一些数据,并将其发送到视图。控制器使事情变得容易。模型/服务处理所有逻辑。您所指的模型只是一小部分。

Now, to address your question, this is something that took me a while to learn. The "Model" that most frameworks talk about in the docs is not all there is to the "Model layer". You can also think of this as a "Service layer". Remember, controllers are "thin", so they should be simple. They should do things like, get input from the request, pass it to someone else to store in the database, get some data, send it to the view. Controllers facilitate things. The Model/Services, handle all of the logic. The "models" you are referring to are just one small piece.

因此,您有一个 BookModel LibraryModel 。但是也许您还有一个 LibraryManager ,它可以跟踪库并在它们之间传递书籍。或 BookParser 会加载一些XML并生成 BookModel 。或 Translator 会拿一本书并将其翻译成另一种语言并制作一本书。

So you have a BookModel and LibraryModel. But maybe you also have a LibraryManager which keeps track libraries and passes books between them. Or a BookParser which will load up some XML and generate a BookModel. Or a Translator which will take a book and translate it into another language and produce a book.

如何很难将模型真正定义为仅1或2个类?一切都取决于您的应用程序。

Do you see how the "model" is hard to really define as just 1 or 2 classes? It all depends on your application.

以这种方式考虑设计阶段。用伪代码编写控制器。例如保存此书,导出书单或从此库中签出一本书。然后,不用考虑框架,只需设计您的业务逻辑来完成这些任务即可。因此,您可以根据需要确定所需的类。您可能可以保留 BookModel LibraryModel 作为表示数据的一种方式。但是您可能需要超越此范围,具体取决于您想对这些数据进行操作。当然,该框架通常会为您处理很多服务层。您可能不必编写类来访问数据库,但可以使用框架的数据库服务对 BookModel 进行CRUD。

Think of the design phase this way. Write controllers with pseudo code. Like "Save this book", or "Export a list of books", or "Check out a book from this library". Then, don't think about the framework, just design your "business logic" to do those tasks. So you figure out what classes you need based on what you need to do. You can probably keep the BookModel and LibraryModel as a way of representing the data. But you will likely need to go beyond that depending on what you want to do with that data. Of course, the framework will often handle a lot of the "service" layer for you. You probably don't have to write the class to access a database, but use your framework's database service to CRUD your BookModel.

另一种OOP原则使类专注于做一件事情(单一职责)。因此,不要让您的 LibraryModel 还负责借出书籍,确定排名前5的书籍以及您想做的所有其他事情。只需专注于存储有关库的数据,然后让另一个类负责该库的操作即可( LibraryLender BookRater ) 。

Another OOP principle make classes focus on doing one thing (Single Responsibility). So don't make your LibraryModel also responsible for lending out books, determining the top 5 books, and every other thing you want to do. Just focus on storing the data about a library and make another class responsible for the actions on it (LibraryLender, BookRater).

好吧,希望这会有所帮助,而不仅仅是荒谬的闲话...

Okay, well, hopefully this is helpful and not just nonsensical ramblings...

这篇关于PHP / CodeIgniter中的对象和模型之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆