merged branch jmikola/patch-3 (PR #3450)

Commits
-------

265360d [DoctrineBridge] Simpler result checking in UniqueEntityValidator

Discussion
----------

[DoctrineBridge] Simpler result checking in UniqueEntityValidator

In 928e352d09, support for MongoDB cursors was implemented by converting an Iterable, non-ArrayAccess object to an array. The ArrayAccess check didn't seem purposeful, since cursors are only Iterable and ORM returns real arrays. Since we only need to access the first element of the cursor (and only in cases where the count is exactly 1), we can simply use current() to handle Iterables and arrays.

@henrikbjorn: Any thoughts on this? I was testing @stof's work in doctrine/DoctrineMongoDBBundle#68 and our Symfony submodule was a bit old, so I fixed UniqueEntityValidator on my local machine before I realized you had come up with a solution a few weeks ago.
This commit is contained in:
Fabien Potencier 2012-03-02 21:39:39 +01:00
commit c4256688d1

View File

@ -94,17 +94,11 @@ class UniqueEntityValidator extends ConstraintValidator
$repository = $em->getRepository($className);
$result = $repository->findBy($criteria);
// MongoDB will return a Cursor so we need to change it to an array
// so it is compatible with the orm returning an array
if ($result instanceof \Iterator && !$result instanceof \ArrayAccess) {
$result = iterator_to_array($result);
}
/* If no entity matched the query criteria or a single entity matched,
* which is the same as the entity being validated, the criteria is
* unique.
*/
if (0 === count($result) || (1 === count($result) && $entity === reset($result))) {
if (0 === count($result) || (1 === count($result) && $entity === current($result))) {
return true;
}