我正在阅读Doctrine 2,我遇到了这篇文章
http://groups.google.com/group/doctrine-dev/browse_thread/thread/3b21fcea5a408aae,其中一个用户想要使用自定义集合扩展PersistantCollection类.在其中,另一个用户响应,
Collections are collections, they hold elements and provide means to
iterate over them or do other typical collection stuff (count, filter
items, add items, remove items, …), always not caring about the
exact nature of the items (products, articles or whatever).
getTotalPrice or getTotalWeight on a collection are completely
misplaced and extending collection classes a similar bad idea in most
situations. It goes against many guidelines, single responsibility
principle being one of them. Put your business logic on your domain
objects/classes themselves, not on the collections. Collections are
just generic data containers.
我的问题是,如果我想对一组书籍对象做一些事情,比如将它们分类并计算每个类别中的数量,那么在集合类中创建一个方法来执行此操作是不正确的吗?或者我应该在实体中创建静态函数来对集合进行排序?我只是不确定我会把这种功能放在哪里….提前感谢您抽出时间阅读这篇文章.干杯!
最佳答案 不,我强烈建议不要使用自定义集合. Collection接口提供了一个完整的公共API来操作集合,无需对集合进行子类化.
在您的类别中,您可以编写一个方法来对$books集合进行排序. …并可能将其挂钩到PostLoad事件中. (你为什么建议使用静态方法?)