php oop MVC 设计 - 应用程序编辑数据的正确架构
现在我已经阅读了大量关于 OOP、MVC 和设计模式的帖子、文章、问题和答案,但我仍然对构建我想要构建的内容的最佳方法有疑问。
我的小框架是以 MVC 方式构建的。它使用 smarty 作为查看器,并且我设置了一个类作为从 url 调用的控制器。
现在我认为我迷失的地方是模型部分。我可能会过多(或过少)混合模型和类/对象。
无论如何,一个例子。当目标是获取驻留在我的数据库中的用户列表时:
应用程序由“users/list”调用,然后控制器运行函数列表,该函数打开“user”类的实例并请求该类从表中检索列表。一旦返回到控制器,控制器通过将结果集(数组)分配给模板并设置模板来将其推送到查看器。 然后,用户单击表中的一行,该行将告诉控制器启动“用户/编辑”,例如,这将创建一个表单并用用户数据填充该表单以供我编辑。
到目前为止,一切都很好。
现在,我将所有这些组合在一个用户类中 - 这样该类将具有 create、getMeAListOfUsers、update 等函数以及 HairType 和 NoseSize 等属性。 但正确的 oop 设计会要求我将“用户”(具有登录名、大鼻子、卷发等属性)与“获取用户列表”分开,感觉更像是“用户管理器类”。
如果我要实现一个用户管理器类,那么它应该是什么样子?它应该是一个对象(无法真正将其与现实世界中的事物进行比较)还是应该是一个仅具有公共函数的类,以便它或多或少看起来像一组函数。
它是否应该返回找到的记录数组(例如:array([0]=>array("firstname"=>"dirk", "lastname"=>"diggler"))
或它是否应该返回一个对象数组?
所有这些对我来说仍然有点令人困惑,我想知道是否有人可以给我一些关于如何以最佳方式实现这一点的见解。
Now that I have read an awfull lot of posts, articles, questions and answers on OOP, MVC and design patterns, I still have questions on what is the best way to build what i want to build.
My little framework is build in an MVC fashion. It uses smarty as the viewer and I have a class set up as the controller that is called from the url.
Now where I think I get lost is in the model part. I might be mixing models and classes/objects to much (or to little).
Anyway an example. When the aim is to get a list of users that reside in my database:
the application is called by e.g. "users/list" The controller then runs the function list, that opens an instance of a class "user" and requests that class to retrieve a list from the table. once returned to the controller, the controller pushes it to the viewer by assigning the result set (an array) to the template and setting the template.
The user would then click on a line in the table that would tell the controler to start "user/edit" for example - which would in return create a form and fill that with the user data for me to edit.
so far so good.
right now i have all of that combined in one user class - so that class would have a function create, getMeAListOfUsers, update etc and properties like hairType and noseSize.
But proper oop design would want me to seperate "user" (with properties like, login name, big nose, curly hair) from "getme a list of users" what would feel more like a "user manager class".
If I would implement a user manager class, how should that look like then? should it be an object (can't really compare it to a real world thing) or should it be an class with just public functions so that it more or less looks like a set of functions.
Should it return an array of found records (like: array([0]=>array("firstname"=>"dirk", "lastname"=>"diggler"))
or should it return an array of objects.
All of that is still a bit confusing to me, and I wonder if anyone can give me a little insight on how to do approach this the best way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的处理和数据(业务逻辑)所需的抽象级别取决于您的需求。例如,对于具有 事务脚本 的应用程序(可能是这种情况)根据您的设计),您描述的从数据库获取和更新数据的类对我来说听起来很有效。
您可以使用表数据网关来概括一些事情, 行数据网关或活动记录甚至。
如果您感觉在事务脚本中复制了大量代码,您可能需要创建自己的 域模型与数据映射器< /a>.然而,我不会从一开始就盲目地这样做,因为这需要更多的代码才能开始。此外,自己编写数据映射器而是使用现有组件也是不明智的。 Doctrine 就是 PHP 中的这样一个组件。
另一个现有的 ORM(对象关系映射器)组件是 Propel,它提供活动记录。
如果您只是在寻找一种快速查询数据库的方法,您可能会发现 NotORM 很有启发。
中找到以斜体列出的模式
列出了书中的所有模式 企业应用模式架构。
The level of abstraction you need for your processing and data (Business Logic) depends on your needs. For example for an application with Transaction Scripts (which probably is the case with your design), the class you describe that fetches and updates the data from the database sounds valid to me.
You can generalize things a bit more by using a Table Data Gateway, Row Data Gateway or Active Record even.
If you get the feeling that you then duplicate a lot of code in your transaction scripts, you might want to create your own Domain Model with a Data Mapper. However, I would not just blindly do this from the beginning because this needs much more code to get started. Also it's not wise to write a Data Mapper on your own but to use an existing component for that. Doctrine is such a component in PHP.
Another existing ORM (Object Relational Mapper) component is Propel which provides Active Records.
If you're just looking for a quick way to query your database, you might find NotORM inspiring.
You can find the Patterns listed in italics in
which lists all patterns in the book Patterns of Enterprise Application Architecture.
我不是这方面的专家,但最近做了几乎完全相同的事情。我的设置方式是,一个类用于多行(
Users
),一个类用于一行(User
)。 “几行类”基本上只是(静态)函数的集合,它们用于从表中检索行,如下所示:返回 User 对象的数组。每个 User 对象都有用于检索表中字段的方法(如 $user->getUsername() 或
$user->getEmail()
等)。我曾经只返回一个关联数组,但后来您遇到了想要在返回数据之前修改数据的情况,这就是为每个字段创建一个包含方法的类非常有意义的地方。编辑:
User
对象还具有更新和删除当前行的方法;I'm not an expert at this but have recently done pretty much exactly the same thing. The way I set it up is that I have one class for several rows (
Users
) and one class for one row (User
). The "several rows class" is basically just a collection of (static) functions and they are used to retrieve row(s) from a table, like so:And that returns an array of User objects. Each User object then has methods for retrieving the fields in the table (like
$user->getUsername()
or$user->getEmail()
etc). I used to just return an associative array but then you run into occasions where you want to modify the data before it is returned and that's where having a class with methods for each field makes a lot of sense.Edit: The
User
object also have methods for updating and deleting the current row;Doctrine 和 Propel 的另一个替代方案是 PHP Activerecords。
Doctrine 和 Propel 确实是强大的野兽。如果你正在做一个较小的项目,我认为你最好选择更轻的东西。
另外,在谈论第三方解决方案时,有很多适用于 PHP 的 MVC 框架,例如:Kohana、Codeigniter、CakePHP、Zend(当然)...
它们都有自己的 ORM 实现,通常是更轻量级的替代方案。
对于 Kohana 框架,还有 Auto modeler ,据说它非常轻量级。
我个人正在使用 Doctrine,但它是一个巨大的项目。如果我要做一些较小的事情,我会尽快选择更轻的替代方案。
Another alternative to Doctrine and Propel is PHP Activerecords.
Doctrine and Propel are really mighty beasts. If you are doing a smaller project, I think you are better off with something lighter.
Also, when talking about third-party solutions there are a lot of MVC frameworks for PHP like: Kohana, Codeigniter, CakePHP, Zend (of course)...
All of them have their own ORM implementations, usually lighter alternatives.
For Kohana framework there is also Auto modeler which is supposedly very lightweight.
Personally I'm using Doctrine, but its a huge project. If I was doing something smaller I'd sooner go with a lighter alternative.