symfony/symfony — Issue #3163: UniqueEntityValidator can't validate values of proxy objects
Author: kevindew
Created: 2012-01-20 19:40:18+00:00
State: open

If you have an association and have that as one of your fields for using the Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator the unique validation wont work if your object is a proxy and hasn't had the identifier field hydrated yet.

I didn't know how to put this into a unit test but did work out a very crude fix in the validator that i'm sure isn't suitable to be a patch but I think helps illustrate the problem better.

```php
if (isset($class->associationMappings[$fieldName])) {

// very crude fix to the problem
if ($criteria[$fieldName] instanceof \Doctrine\ORM\Proxy\Proxy) {
$criteria[$fieldName]->__load();
}

$relatedClass = $em->getClassMetadata($class->associationMappings[$fieldName]['targetEntity']);
$relatedId = $relatedClass->getIdentifierValues($criteria[$fieldName]);

if (count($relatedId) > 1) {
throw new ConstraintDefinitionException(sprintf('Associated entities are not allowed to have more than one identifier field to be part of a unique constraint in %s#%s.', $class->name, $fieldName));
}
$criteria[$fieldName] = array_pop($relatedId);
}

```

I experienced this on Symfony 2.0.9, running on OS X Lion