doctrine/DoctrineModule — pull request #103 by bakura10 (2012-10-10 20:01:56+00:00)
Title: Add more merging collection strategies

@davidwindall, @ocramius

This PR tries to provide a solution to the problem that was highlighted here in the comments : https://github.com/doctrine/DoctrineModule/pull/96

For this solution I added two more strategies to the existing (and likely most useful) intersectionUnion:

- union: it's the use case @davidwindall described, so that even if the new collection that is posted contains less elements than the one already existing, the new ones are added, but the existing one are still preserved even if they are not here in the new collection.
- intersection: this strategy only keeps the elements that are BOTH in the old collection and the new one that is submitted. I can't see an immediate use case of this one, but it may... dunno
- intersectionUnion: this is the existing one, it's like intersection but it preserves the ones that exist in the new collection, even if they does not exist in the old one.

By default, the strategy applied to every collection is the intersectionUnion. In the case of david and its attachments/post use case, you would specify to the hydrator to use the "union" strategy for the collections "attachments" :

```php
$hydrator->setCollectionMergingStrategy(DoctrineObject::COLLECTION_MERGING_UNION, 'attachments');
```

It will tell to the hydrator to use the other strategy for the 'attachments' collection (but still using the intersectionunion strategy for all one. If you want to set the same strategies for all collections:

```php
$hydrator->setCollectionMergingStrategy(DoctrineObject::COLLECTION_MERGING_UNION, '*');
```