Zend_Paginator 模糊 MVC 线条
我正在开发一个 Zend Framework (1.7) 项目,其结构松散地基于快速启动应用程序的结构 - 前端控制器、操作控制器、视图和视图。 使用 Zend_Db_Table 访问数据库的模型。 我的一个主要模型依赖于一些昂贵的联接来拉出其主要列表,因此我正在考虑使用 Zend_Paginator 来减少从数据库带回的行数。 我的问题是 Zend_Paginator 仅附带 4 个适配器,其中没有一个真正适合我。
- Array:构建数组以提供给 ZP 将涉及获取所有记录,这是我试图避免的
- Iterator:一个愚蠢的迭代器会带来与数组和聪明的人感觉它不太适合模型
- DbSelect :将 DbSelect 对象获取到控制器会将控制器与我的数据库的内部工作联系起来(更不用说生成原始结果行而不是封装的对象)
- DbTableSelect :与 DbSelect 相同
- Null Adapter :手动来回传递所有详细信息。
将分页器传递到模型中感觉也会违反 MVC 分离。 问题是我错误地构建了模型,还是我在维护 MVC 分离方面过于教条,或者我是否缺少一种将所有活动部件粘在一起的干净、优雅的方法?
I'm working on a Zend Framework (1.7) project with a structure loosely based on the structure of the quickstart application - front controller, action controllers, views & models that use Zend_Db_Table to get to the database. One of my main models relies on some expensive joins to pull up its primary listing, so I'm looking into using Zend_Paginator to reduce the number of rows brought back from the database. My problem is that Zend_Paginator only comes with 4 adaptors, none of which really seem to be a good fit for me.
- Array : Building the array to feed to ZP would involve fetching all the records which is what I'm trying to avoid
- Iterator : A dumb iterator would present the same problems as an array and a smart one feels like it would be a poor fit for the Model
- DbSelect : Getting a DbSelect object up to the Controller would uncomfortably tie the Controller to the inner workings of my DB (not to mention producing raw result rows rather than encapsulated objects)
- DbTableSelect : same as DbSelect
- Null Adapter : pass all the details back and forth manually.
Passing the paginator into the model feels like it, too, would violate the MVC separation. Is the problem that I've built my model incorrectly, that I'm being to dogmatic about maintaining MVC separation or am I missing a clean, elegant way of sticking all the moving parts together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以在模型上提供一个接口,该接口接受
$current_page
和$per_page
参数并返回当前页面的数据集以及分页器对象。这样,您的所有分页代码都包含在模型中,并且您可以自由使用数据库适配器,而不会觉得自己破坏了概念。
另外,控制器实际上不应该设置寻呼机,因为您将其与数据绑定在一起是正确的(并且模型用于数据,而不仅仅是数据库连接)。
You can provide an interface on your models that accepts
$current_page
and$per_page
parameters and returns the current page's data set as well as a paginator object.This way all your pagination code is contained within the model and you are free to use the Db adapters without feeling like you've broken the concept.
Plus, the controller really shouldn't be setting up the pager anyways since you are correct in it being tied to the data (and models are for data, not just database connections).
Zend_Paginator 现在有一个 setFilter 方法,允许您将数据从 row 对象加载到您想要的任何模型对象:
There is now a setFilter method for Zend_Paginator that allows you to load the data from the row object to any model object you want:
我确实需要一个解决方案,在该解决方案中我可以使用 Zend_Db_Table 类方法作为我的分页器适配器的资源,而不是数组或 Zend_Db_Select 对象。
这种高级建模与 Zend_Paginator 的标准适配器不兼容。 我继续为每个像我一样渴望答案的人解决了这个问题。
这就是它在控制器中的工作方式:
此适配器工作的唯一要求是在模型方法参数列表中列出以下参数(以任何顺序[适配器将通过反射检测方法签名):
$limit = 0, $offset = 0, $count = false
分页器将使用 $limit、$offset 和 $count 参数的适当值来调用您的方法。 就是这样!
例子:
I really needed a solution in which I could use a Zend_Db_Table class method as the resource for my paginator adapter, instead of an array or a Zend_Db_Select object.
This sort of advanced modeling is not compatible with the standard adapters for Zend_Paginator. I went ahead and fixed this for everyone who is desperate for an answer, as I was.
This is how it works in your controller:
The only requirements for this adapter to work is to list the following parameters in your model methods arguments list (in any order [the adapter will detect the method signature through reflection):
$limit = 0, $offset = 0, $count = false
The paginator will call your method with the appropriate values for the $limit, $offset, and $count arguments. That's it!
Example:
好吧,我无法回答您对使用 DbSelect 的担忧,但我确实遇到了这段与减少拉取行数问题相关的代码(在 ibuildings 博客的评论中)。 可能对某些读者有用。
Well, I can't give you an answer to your concerns with using DbSelect but I did come across this bit of code (in the comments of the ibuildings blog) relating to the issue of reducing the number of rows pulled. Might be useful to some readers.
使用 MVC 时要考虑的一个重要考虑因素是模型适用于所有域逻辑,而控制器适用于业务逻辑。 一般的经验法则是模型应该不了解接口(控制器或视图),但它们不必是简单的数据库访问器。 为了尽可能可移植,他们应该对格式或显示属性一无所知(除非这是域逻辑的一部分)。
事实上,操作域逻辑的所有逻辑都应该位于模型中,而不是位于控制器中。 控制器应该将信息从界面传递到模型,根据需要将其转换,并选择要显示/更新的视图。 如果它与界面没有任何关系,那么它可能会更好地在模型中表示,而不是在控制器中表示,因为如果您决定稍后交换控制器/视图配对,它将允许更多的代码重用。
理想情况下,您的模型应该提供一个用于访问所需信息的界面。 只要模型不知道 MVC 的 VC 部分,该接口背后如何实现就不是 MVC 关心的问题。 如果这意味着传递一个分页器对象,那么这并不直接违反 MVC 原则,但如果分页器与渲染本身有关(抱歉,我不知道 Zend),那么最好传递一个接口它的(缺少渲染方法),让模型操作/填充它,然后将其传回。 这样,您就不会从模型生成渲染代码,并且如果您决定稍后将应用程序设为控制台应用程序(或添加某种 API 接口),则可以替换分页器实现。
An important consideration to take into account when working with MVC is that the model is for all domain logic, whereas the controller is for the business logic. A general rule of thumb is that the model(s) should have no knowledge of the interface (controllers or views), but they need not be simple DB accessors. To be as portable as possible, they should know nothing of formatting or display properties either (unless that is part of the domain logic).
In fact, all logic that manipulates domain logic should be in the model and not in the controllers. The controller should pass information from the interface, transforming it as needed, to the model, and select which view to display/update. If it doesn't have anything to do with the interface, it might better be represented in a model than in a controller, as it would allow for more code reuse if you decide to swap out the controller/view pairings later.
Ideally, your model should provide an interface for accessing the information you need. How that is implemented behind that interface is not a concern of MVC, so long as the model remains unaware of the VC portion of MVC. If that means passing around a paginator object, that is not a direct violation of the MVC principles, though if the paginator has anything to do with rendering itself (sorry, I don't know Zend), it might be best to pass an interface of it in (that is missing the rendering methods), have the model manipulate/fill it in, then pass it back out. That way you don't have rendering code being generated from the model, and you could replace the paginator implementation if you decided to make your application a console app later (or add an API interface of some sort).
如果您使用 DbSelect 适配器,您可以简单地传入结果集,这对于保持一定的分离有很大帮助。 因此,在您的控制器中:
在视图中,您可以通过分页器访问数据
这是一个简化的示例(我还在引导程序中设置了默认滚动样式和默认视图部分),但我认为在控制器中设置它不是不好,因为从模型检索的数据无论如何都会被控制器放入视图中,并且此实现使用结果集而不是模型。
If you use the DbSelect adapter you can simply pass in the resultset and this goes a long way in maintaining some separation. So in your controller:
In the view you can access the data through the paginator
This is a simplified example (I also setup up default scrolling style and default view partial in the bootstrap), but I thnk setting it up in the controller isn't bad because the data retrieved from the model is placed into the view by the Controller anyway and this implementation makes use of the resultset NOT the model.
您还可以直接实现 Zend_Paginator_Adapter_Interface 或在任何需要支持分页的模型中扩展 Zend_Paginator_Adapter_DbSelect。
这样,模型不直接了解有关 View、Controller 甚至 Zend_Paginator 的任何信息,但可以在最有意义的地方直接与 Zend_Paginator 一起使用。
有了这样的东西,您可以在最有意义的地方使用此类的实例直接实例化寻呼机:
现在,您可以使用此类作为任何集合“模型”的基类。
You could also implement the Zend_Paginator_Adapter_Interface directly or extend Zend_Paginator_Adapter_DbSelect in any model that needs to support pagination.
That way, the model doesn't directly know anything about View, Controller or even the Zend_Paginator, but can be directly used with the Zend_Paginator wherever it makes the most sense.
With something like this, you can directly instantiate a pager using an instance of this class wherever it makes the most sense:
Now, you can use this class as the base class for any "Model" that is a set.