merged branch henrikbjorn/doctrine-unique-validator (PR #3275)

Commits
-------

928e352 Change the array access used in UniqueEntityValidator

Discussion
----------

[DoctrineBridge] Change the array access used in UniqueEntityValidator

MongoDB ODM Cursor does not implement ArrayAccess and therefor using
`$result[0]` will fail. `reset()` rewinds the array and returns the
first element value.

DoctrineMongoDBBundle#77

/cc @beberlei

---------------------------------------------------------------------------

by henrikbjorn at 2012-02-06T08:09:25Z

Made a mistake, the findBy call would still return a cursor. Changed the findBy to findOneBy we only want one result anyway.

---------------------------------------------------------------------------

by stof at 2012-02-06T08:11:26Z

findOneBy is wrong: you will not detect duplicate anymore if you return a single element

---------------------------------------------------------------------------

by henrikbjorn at 2012-02-06T08:28:03Z

@stof before it was only the first result that was used anyway so if it had found 3 results it would only have used the first one. Performance is apparently the biggest issue with findOneBy so this have been reverted.

---------------------------------------------------------------------------

by stof at 2012-02-06T08:31:17Z

@henrikbjorn no, we use other results too: if ``count()`` is not 1, the validation fails

---------------------------------------------------------------------------

by stof at 2012-02-06T08:36:06Z

Btw, running the testsuite would have tell you there is an issue when using findOneBy as it was breaking a test :)

---------------------------------------------------------------------------

by bschussek at 2012-02-06T10:45:44Z

Why doesn't this validator use a SELECT COUNT anyway?

---------------------------------------------------------------------------

by stof at 2012-02-06T10:57:00Z

@bschussek because we need to check if an existing value is for the same object or another one. Otherwise, the valdiation would fail as soon as you are editing an existing object without changing the unique value

---------------------------------------------------------------------------

by henrikbjorn at 2012-02-06T11:03:40Z

@stof @bschussek other changes that should be done? Else it should be ready to be merged ?

---------------------------------------------------------------------------

by henrikbjorn at 2012-02-06T13:00:57Z

@stof done an rebased.

---------------------------------------------------------------------------

by bschussek at 2012-02-06T13:04:44Z

👍 /cc @fabpot
This commit is contained in:
Fabien Potencier 2012-02-06 14:06:46 +01:00
commit aa1676c7b5
1 changed files with 7 additions and 1 deletions

View File

@ -94,11 +94,17 @@ 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 === $result[0])) {
if (0 === count($result) || (1 === count($result) && $entity === reset($result))) {
return true;
}