CodeIgniter MVC 模型逻辑
我曾使用 Rails、Django、Zend 和 CakePHP 进行编程。另外,还有 Wordpress 和 Drupal。
现在,我正在 CodeIgniter 中相当大的应用程序中“赶上速度”。
通常,我使用 MVC 框架的经验使我相信模型代表引用实际数据库表的业务逻辑。在 CodeIgniter 文档和我正在剖析的代码库中,我看到创建的模型代表了像页面一样模糊的东西。许多业务逻辑直接写入这些模型中,并且它们合并了其他实际数据模型。我不确定这是否理想并遵循 MVC。
是否应该在数据模型之外创建模型?
另外,假设我有一个代表用户的数据模型(数据库中的user
表)。在该用户表中,有一个名为 gender
的列,其类型为 enum('male', 'female')。现在我想用枚举列中的性别选项填充下拉列表。
这个逻辑放在哪里最合适呢?
用户模型是用于表示数据库中的单个用户或行的对象......对吗?因此,在用户模型/类中包含一个名为“get_gender_options”的函数似乎并不合适,因为虽然该函数与用户表相关,但它与单个用户对象无关。在 Zend 中,这种逻辑可以内置到表单对象本身中。
没有“正确”的答案,只有我们认为最合适的答案......
I have programmed in Rails, Django, Zend, and CakePHP. Also, Wordpress and Drupal.
Now, I am "catching up to speed" in as fairly large application in CodeIgniter.
Typically, my experience with MVC frameworks, has led me to believe that Models represent business logic in reference to actual database tables. In the CodeIgniter docs and in the code base I'm dissecting I see models created that represent something as vague as a page. A lot of the business logic is written directly into those models and they incorporate other actual data models. I'm not sure this is ideal and following MVC.
Should models be created beyond data models?
Also, lets say I have a data model representing a user (user
table in DB). In that user table there is a column called gender
of the type enum('male', 'female'). Now I want to populate a dropdown with the gender options from the enum column.
Where is it most appropriate to put this logic?
The user model is an object used to represent a single user or row in the db... right? So it doesn't seem fitting to include a function in the user model/class called "get_gender_options", because, while the function is related to the user table, it is NOT relevant to a single user object. In Zend this kind of logic could be built into the form object itself.
There is not "right" answer, just one we can consider the most appropriate...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能只是将“get_gender_options”放入模型中,而不是将其粘贴在表单的视图中。为了保持它干燥但不将其放入模型中,我将创建一个助手来保存它。
I would probably just put the "get_gender_options" in the model, rather than sticking it in the view for the form. To keep it DRY but not put it in the model, I would create a helper to hold this.