Reputation: 52568
When creating a collection inside an entity you initialise it as an ArrayCollection
in the constructor. ArrayCollection
can be extended and those children used in it's place. Howerver, When the entity is retrieved from the database the ArrayCollection
is swapped for a PersistentCollection
which implements the same interface as ArrayCollection
but is marked as final
.
Is there a way of implementing your own collection class that when retrieving data from the database allows you to use custom collection methods for sorting and retrieving items from the collection.
P.S. I don't want to modify the Doctrine source itself.
Upvotes: 2
Views: 202
Reputation: 146
What i did was is modify my public getCollection method.
private $collectionOfStuff; //this is a subclassed Doctrine\Common\Collections\ArrayCollection (MyCustomCollectionType in this case)
public function getCollectionOfStuff() {
if (!$this->collectionOfStuff instanceof MyCustomCollectionType) {
$this->collectionOfStuff = new MyCustomCollectionType($this->collectionOfStuff->toArray());
}
return $this->collectionOfStuff;
}
Afterwards you'll have to use $this->getCollectionOfStuff() to work your magic.
It is not the best solution (i've could've build a proxy or some sorts) but as subclassing the ArrayCollection is not supported by doctrine yet, it was for me the fastest/easiest fix.
Upvotes: 1