简体   繁体   English

是否应该在 Model 或 Controller 中获取相关行?

[英]Should dependent rows be fetched in the Model or the Controller?

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 .有四个表, EmployeeStoreDepartmentEmployee_Department_Permissions

Departments belong to Stores (for example, the Department table might contain a record for the dairy department of store 5).部门属于商店(例如,部门表可能包含商店 5 的乳制品部门的记录)。 Employees are given the right to work in Departments through the Employee_Department_Permissions table, which contains the Employee id and Department id.员工通过 Employee_Department_Permissions 表获得在部门中工作的权利,该表包含员工 ID 和部门 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.我有一个 Employee model 和一个映射器,它提供fetchAll方法来完成第一部分。

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.在我的 model 包装器中,我可以调用findDependentRows来执行此操作。 Or, I could do it in my controller with raw Zend_Db_Select calls.或者,我可以在我的 controller 中使用原始Zend_Db_Select调用来实现。 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.我正在考虑的第三个选项是在 Employee 表中添加一个包含此信息的列,但是当 Employee_Department_Permission 被修改时,我需要更新第二个表。

Thank you in advance for any advice.提前感谢您的任何建议。

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.作为一个非常普遍的经验法则,我建议您尽量让 controller 尽可能免于获取视图信息。 This is a task best handled in the model.这是在 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.当然,从 controller 获取很容易,我的意思是,因为我们在那里处理请求,所以只需快速获取并将其推送到视图就很简单了。 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.我的 2 美分,祝你朋友编码愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM