应该在模型还是控制器中获取依赖行?
让我从一个简单的示例开始,展示我的数据的结构。有四个表:Employee
、Store
、Department
和 Employee_Department_Permissions
。
部门属于商店(例如,部门表可能包含商店 5 的乳制品部门的记录)。通过 Employee_Department_Permissions 表授予员工在部门工作的权限,该表包含员工 ID 和部门 ID。
假设一名员工可以登录我的应用程序并查看数据库中每个商店的列表。在每个商店旁边,我想打印出每个商店可以在多少个部门工作。我有一个带有映射器的 Employee 模型,它提供 fetchAll 方法来完成第一部分。
但是,我应该在哪里知道一个员工可以在多少个部门工作呢?在我的模型包装器中,我可以调用 findDependentRows 来执行此操作。或者,我可以在控制器中使用原始 Zend_Db_Select
调用来完成此操作。我正在考虑的第三个选项是仅向 Employee 表添加一列来保存此信息,但是当修改 Employee_Department_Permission 时,我需要更新第二个表。
预先感谢您的任何建议。
Let me start with a simple example to show how my data is structured. There are four tables, Employee
, Store
, Department
, and Employee_Department_Permissions
.
Departments belong to Stores (for example, the Department table might contain a record for the dairy department of store 5). Employees are given the right to work in Departments through the Employee_Department_Permissions table, which contains the Employee id and Department id.
Let's say an employee can log in to my application and view a list of every store in the database. Next to each store, I want to print out how many departments they can work in at each store. I have an Employee model with a mapper that provides the fetchAll
method to accomplish the first part.
But, where should I find out how many departments an employee can work in? In my model wrapper, I can call findDependentRows
to do this. Or, I could do it in my controller with raw Zend_Db_Select
calls. A third option I was considering would to be just add a column to the Employee table that holds this information, but then I'd need to update a second table when Employee_Department_Permission is modified.
Thank you in advance for any advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为一个非常普遍的经验法则,我建议您尝试让控制器尽可能自由地获取视图信息。这是模型中最好处理的任务。
当然,从控制器获取数据很容易,我的意思是,因为我们在那里处理请求,所以只需快速获取并将其推送到视图就非常简单。 这就是纪律发挥作用的地方。随着您的应用程序的增长,您将欣赏这种方法为您提供的干净分离如果应用。
我的 2 美分,祝你朋友编码愉快。
As a very general rule of thumb, I would suggest you try keep the controller as free as possible from fetching information for the views. This is a task best handled in the model.
Sure it's easy to just fetch from controller, I mean, since we are there processing a request, it would be so simple to just do a quick fetch and push that off to the view. This is where dicipline comes into play. As your application grows you will appreciate having the clean separation this methodology offers you if applied.
My 2 cents, happy coding to you friend.