应该在模型或控制器中获取相关行吗? [英] Should dependent rows be fetched in the Model or the Controller?

查看:77
本文介绍了应该在模型或控制器中获取相关行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我从一个简单的示例开始,展示我的数据的结构.有四个表,EmployeeStoreDepartmentEmployee_Department_Permissions.

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.

部门属于商店(例如,部门"表可能包含商店5的乳品部门的记录).通过Employee_Department_Permissions表,该表授予员工在Departments中工作的权利.

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.

比方说,一名员工可以登录到我的应用程序并查看数据库中每个商店的列表.在每个商店旁边,我想打印出每个商店可以在多少个部门工作.我有一个带有映射器的Employee模型,该模型提供了fetchAll方法来完成第一部分.

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.

但是,我应该在哪里找出员工可以在多少个部门工作?在模型包装器中,我可以调用findDependentRows来执行此操作.或者,我可以在控制器中通过原始的Zend_Db_Select调用来做到这一点.我正在考虑的第三个选项是将一个列添加到保存此信息的Employee表中,但是当Employee_Department_Permission修改后,我需要更新第二个表.

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.

推荐答案

作为一个非常普遍的经验法则,我建议您尝试使控制器尽可能免于获取视图信息.这是模型中最好处理的任务.

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.

我的2美分,很高兴为您的朋友编码.

My 2 cents, happy coding to you friend.

这篇关于应该在模型或控制器中获取相关行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆