Search code examples
phpcollectionsdoctrine-ormodm

How to specify the class for Doctrine ODM Collection mapping types


I have a document with a field marked for persistence as a Collection

 use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;  

/** @ODM\Document */
class Item
{
    /**
    * @ODM\Collection
    */
    protected $things;

}

By default, when Doctrine maps the values from Mongo it will set $things to be an instance of Doctrine\Common\Collections\ArrayCollection.

This violates my domain model however as my domain objects expect the $things property to be an instance of my own collection class ThingsCollection. This class does various validations on the list of things.

How can I tell Doctrine to use my ThingsCollection class instead? I imagine I will have to make the ThingsCollection class implement the Doctrine\Common\Collections\Collection interface but that is not a problem if I can just figure out how to tell Doctrine about the mapping in the first place.


Solution

  • The answer is that it is not possible.

    Furthermore, despite the documentation being confusing and saying otherwise, for a general Collection property mapping as above (not a reference) an ArrayCollection is not returned. A basic php array is given.

    So, it appears in this situation, you are forced to work with arrays, at least currently.