PHP MVC - 模型列表?
我刚开始使用 MVC 结构,并且正在为一个大学项目开发自己的 MVC 框架。我有一个数据库类,可以使用它向数据库发送查询并返回一个对象数组(PHP 标准对象类)。然后我想在索引页上显示对象列表。
我的问题是,这个标准对象列表真的应该是模型列表吗?或者他们本来就很好吗?
I'm new to using an MVC structure, and am developing my own MVC framework for a university project. I've got a database class that I can use to send a query to the database and return me an array of objects (PHP standard object class). I then want to display a list of the objects on an index page.
My question is, should this list of standard objects really be a list of models? Or are they fine as they are?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不必仅仅为了创建模型类而创建单独的模型类,因为它们位于 MVC 模式名称中。如果您的解决方案在您只需将 PHP 标准对象类数组传递到视图时也能正常工作,那么您应该使用它。在这种情况下,创建模型类只是额外的工作,没有任何好处。但是,如果您除了简单地输出数据库结果列表之外还需要其他功能,则应该考虑创建实际模型。
You don't have to create separate model classes just for the sake of creating model classes because they are in the MVC pattern name. If your solution also works when you simply pass the array of PHP standard object classes to your view, you should just use that. In that case creating model classes would just be extra work for no benefit. However, if you need additional functionality besides simply outputting a list of database results, you should consider creating actual models.
您应该将数据存储在数据库中并使用模型操作这些数据(创建、更新、读取和销毁)。我认为你应该更好地了解什么是 MVC 以及它的用途。您可以在此处阅读一些内容
You should store data in your database and manipulate this data (create,update,read and destroy) with your models. I think you should better know what is MVC and what is for. You can read a bit here
取决于...有人可能会说,大多数应用程序在模型方面非常繁重(胖模型是一种实际模式),因此从数组创建一些 stdObjects 是不够的,但实际上将表映射到对象类,所以您可以向它们添加有用的方法。
我建议查看 Doctrine 并尝试实现一部分功能。
depends... One could argue that most applications are very heavy on the Model side (Fat Models is an actual pattern), therefore it will not be enough to create a few stdObjects from an array, but actually map tables to object classes, so you can add useful methods to them.
I would recommend to take a look at Doctrine and try implementing a subset of features.