bug #17355 [DoctrineBridge][Validator] >= 2.3 Pass association instead of ID as argument (xavismeh)

This PR was squashed before being merged into the 2.3 branch (closes #17355).

Discussion
----------

[DoctrineBridge][Validator] >= 2.3 Pass association instead of ID as argument

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

No verification on doctrine-orm version has been provided since the `composer.json` requires `"doctrine/orm": "~2.4,>=2.4.5"` and performing a query with an object as array argument has been added in 2.2.0-BETA1 (see https://github.com/doctrine/doctrine2/blob/2.2.0-BETA1/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php#L1511)

Commits
-------

5c2d534 [DoctrineBridge][Validator] >= 2.3 Pass association instead of ID as argument
This commit is contained in:
Fabien Potencier 2016-01-25 12:44:21 +01:00
commit a52ae99735
2 changed files with 1 additions and 36 deletions

View File

@ -16,7 +16,6 @@ use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Persistence\ObjectRepository;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity;
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
@ -398,7 +397,7 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
$this->buildViolation('myMessage')
->atPath('property.path.single')
->setInvalidValue(1)
->setInvalidValue($entity1)
->assertRaised();
}
@ -422,29 +421,6 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
$this->assertNoViolation();
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage Associated entities are not allowed to have more than one identifier field
*/
public function testAssociatedCompositeEntity()
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('composite'),
'em' => self::EM_NAME,
));
$composite = new CompositeIntIdEntity(1, 1, 'test');
$associated = new AssociationEntity();
$associated->composite = $composite;
$this->em->persist($composite);
$this->em->persist($associated);
$this->em->flush();
$this->validator->validate($associated, $constraint);
}
/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
* @expectedExceptionMessage Object manager "foo" does not exist.

View File

@ -92,17 +92,6 @@ class UniqueEntityValidator extends ConstraintValidator
* getter methods in the Proxy are being bypassed.
*/
$em->initializeObject($criteria[$fieldName]);
$relatedClass = $em->getClassMetadata($class->getAssociationTargetClass($fieldName));
$relatedId = $relatedClass->getIdentifierValues($criteria[$fieldName]);
if (count($relatedId) > 1) {
throw new ConstraintDefinitionException(
'Associated entities are not allowed to have more than one identifier field to be '.
'part of a unique constraint in: '.$class->getName().'#'.$fieldName
);
}
$criteria[$fieldName] = array_pop($relatedId);
}
}