Doctrine 2 asks me about the purpose of collections, what methods can I get in collections in general?

I read in Doctrine 2 and I came across this post http://groups.google.com/group/doctrine-dev/browse_thread/thread/3b21fcea5a408aae in which the user wanted to extend the PersistantCollection class with a user collection. In it, another user answers,

Collections are collections, they contain elements and provide a means to iterate over them or make other typical materials for collections (invoice, filter
items, add items, delete items, ...), always not caring about
the exact nature of the items (products, products, or any other).
getTotalPrice or getTotalWeight in the collection is fully
inappropriately and extends collection classes a similarly bad idea in most situations. This is contrary to many recommendations, a single responsibility
principle is one of them. Put your business logic in your domain
objects / classes, not collections. Collections
just common data containers.

My question is: if I wanted to do something with a collection of book objects, for example, sort them by categories and count the number in each category, would it be wrong to create a method in the collection class for this? Or should I make a static function inside the object to sort the collection? I'm just not quite sure where I would put this type of function ... Thanks in advance for taking the time to read this post. Hooray!

+4
source share
1 answer

No, I would highly recommend against user collections. The Collection interface provides a complete public API for managing the collection anyway, eliminating the need to subclass the collection.

In your Category you can write a method that sorts the collections of $books .... And, possibly, connect it to the PostLoad event. (Why did you suggest the static method?)

+2
source

All Articles