You are browsing a version that is no longer maintained.

Custom Collections

This feature was introduced in version 1.1

By default, Doctrine uses ArrayCollection implementation of its Collection interface to hold both embedded and referenced documents. That collection may then be wrapped by a PersistentCollection to allow for change tracking and other persistence-related features.

1<?php use Doctrine\Common\Collections\ArrayCollection; /** @Document */ class Application { // ... /** * @EmbedMany(targetDocument="Section") */ private $sections; public function __construct() { $this->sections = new ArrayCollection(); } // ... }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

For most cases this solution is sufficient but more sophisticated domains could use their own collections (e.g. a collection that ensures its contained objects are sorted) or to simply add common filtering methods that otherwise would otherwise be added to owning document's class.

Custom Collection Classes

You may want to check malarzm/collections which provides alternative implementations of Doctrine's Collection interface and aims to kickstart development of your own collections.

Using your own Collection implementation is as simple as specifying the collectionClass parameter in the @EmbedMany or @ReferenceMany mapping and ensuring that your custom class is initialized in the owning class' constructor:

1<?php use Doctrine\Common\Collections\ArrayCollection; /** @Document */ class Application { // ... /** * @EmbedMany( * collectionClass="SectionCollection" * targetDocument="Section" * ) */ private $sections; public function __construct() { $this->sections = new SectionCollection(); } // ... }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

If you are satisfied with ArrayCollection and only want to sprinkle it with some filtering methods, you may just extend it:

1<?php use Doctrine\Common\Collections\ArrayCollection; class SectionCollection extends ArrayCollection { public function getEnabled() { return $this->filter(function(Section $s) { return $s->isEnabled(); }); } }
2
3
4
5
6
7
8
9
10
11
12
13

Alternatively, you may want to implement the whole class from scratch:

1<?php use Doctrine\Common\Collections\Collection; class SectionCollection implements Collection { private $elements = array(); public function __construct(array $elements = array()) { $this->elements = $elements; } // your implementation of all methods interface requires }
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Taking Control of the Collection's Constructor

By default, Doctrine assumes that it can instantiate your collections in same manner as an ArrayCollection (i.e. the only parameter is an optional PHP array); however, you may want to inject additional dependencies into your custom collection class(es). This will require you to create a PersistentCollectionFactory implementation, which Doctrine will then use to construct its persistent collections. You may decide to implement this class from scratch or extend our AbstractPersistentCollectionFactory:

1<?php use Doctrine\ODM\MongoDB\PersistentCollection\AbstractPersistentCollectionFactory; use Symfony\Component\EventDispatcher\EventDispatcherInterface; final class YourPersistentCollectionFactory extends AbstractPersistentCollectionFactory { private $eventDispatcher; public function __construct(EventDispatcherInterface $eventDispatcher) { $this->eventDispatcher = $eventDispatcher; } protected function createCollectionClass($collectionClass) { switch ($collectionClass) { case SectionCollection::class: return new $collectionClass(array(), $this->eventDispatcher); default: return new $collectionClass; } } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

The factory class must then be registered in the Configuration:

1<?php $eventDispatcher = $container->get('event_dispatcher'); $collFactory = new YourPersistentCollectionFactory($eventDispatcher); $configuration = new Configuration(); // your other config here $configuration->setPersistentCollectionFactory($collFactory);
2
3
4
5
6
7