symfony/symfony — pull request #9848 by arvenil (2013-12-23 16:18:06+00:00)
Title: UniqueEntity failed to invalidate new collections with duplicate values

Basically I run over the same problem as described here:
http://stackoverflow.com/questions/11782056/how-to-validate-unique-entities-in-an-entity-collection-in-symfony2
In other words, if you have new entities collection and it contains at least two objects with same values on unique field then validator does nothing, allowing to proceed orm with inserting data which ends with "SQLSTATE[23000]: Integrity constraint violation".
The problem is that validator is unaware of collection as a whole, and it only validates against db.

So I thought I could try to fix even though problem is not trivial and solution I came with isn't great too (again, because validators are unaware of collection as a whole). But without it UniqueEntity is kinda useless right now.

The most important part is testValidateUniquenessForDuplicatesInCollection. However additionally I've changed tests to actually use entity with defined unique constraint, so test could be more reliable. Test testValidateUniquenessAfterConsideringMultipleQueryResults immediately failed after that change as it is broken by design. You can't persist record "foo" for field name twice if assumption was that field name is unique - so I fixed that test too.

Anyway, feel free to merge that, change whatever need changes (or ask me to do that) or point me to completely different solution.