[Doctrine] [Bridge] Add a way to select the repository used by the UniqueEntity validator
The new option expects an entity path, in order to select its repository. This allows to properly use the UniqueEntity constraint on hierarchical (inheritance) entities, using the root class repository in order to check the uniqueness, and not just the child repository.
This commit is contained in:
parent
031b85029e
commit
00d54597ca
19
src/Symfony/Bridge/Doctrine/Tests/Fixtures/Employee.php
Normal file
19
src/Symfony/Bridge/Doctrine/Tests/Fixtures/Employee.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
|
||||
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
|
||||
/** @Entity */
|
||||
class Employee extends Person
|
||||
{
|
||||
}
|
45
src/Symfony/Bridge/Doctrine/Tests/Fixtures/Person.php
Normal file
45
src/Symfony/Bridge/Doctrine/Tests/Fixtures/Person.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
|
||||
|
||||
use Doctrine\ORM\Mapping\DiscriminatorColumn;
|
||||
use Doctrine\ORM\Mapping\DiscriminatorMap;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\InheritanceType;
|
||||
|
||||
/**
|
||||
* @Entity
|
||||
* @InheritanceType("SINGLE_TABLE")
|
||||
* @DiscriminatorColumn(name="discr", type="string")
|
||||
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
|
||||
*/
|
||||
class Person
|
||||
{
|
||||
/** @Id @Column(type="integer") */
|
||||
protected $id;
|
||||
|
||||
/** @Column(type="string") */
|
||||
public $name;
|
||||
|
||||
public function __construct($id, $name)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return (string) $this->name;
|
||||
}
|
||||
}
|
@ -16,6 +16,8 @@ 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\Employee;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\Person;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleNameEntity;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
|
||||
@ -134,6 +136,8 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2'),
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\Person'),
|
||||
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\Employee'),
|
||||
));
|
||||
}
|
||||
|
||||
@ -517,4 +521,54 @@ class UniqueEntityValidatorTest extends AbstractConstraintValidatorTest
|
||||
|
||||
$this->validator->validate($entity, $constraint);
|
||||
}
|
||||
|
||||
public function testValidateInheritanceUniqueness()
|
||||
{
|
||||
$constraint = new UniqueEntity(array(
|
||||
'message' => 'myMessage',
|
||||
'fields' => array('name'),
|
||||
'em' => self::EM_NAME,
|
||||
'entityClass' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\Person',
|
||||
));
|
||||
|
||||
$entity1 = new Person(1, 'Foo');
|
||||
$entity2 = new Employee(2, 'Foo');
|
||||
|
||||
$this->validator->validate($entity1, $constraint);
|
||||
|
||||
$this->assertNoViolation();
|
||||
|
||||
$this->em->persist($entity1);
|
||||
$this->em->flush();
|
||||
|
||||
$this->validator->validate($entity1, $constraint);
|
||||
|
||||
$this->assertNoViolation();
|
||||
|
||||
$this->validator->validate($entity2, $constraint);
|
||||
|
||||
$this->buildViolation('myMessage')
|
||||
->atPath('property.path.name')
|
||||
->setInvalidValue('Foo')
|
||||
->setCode('23bd9dbf-6b9b-41cd-a99e-4844bcf3077f')
|
||||
->setParameters(array('{{ value }}' => 'Foo'))
|
||||
->assertRaised();
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
|
||||
* @expectedExceptionMessage The "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity" entity repository does not support the "Symfony\Bridge\Doctrine\Tests\Fixtures\Person" entity. The entity should be an instance of or extend "Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity".
|
||||
*/
|
||||
public function testInvalidateRepositoryForInheritance()
|
||||
{
|
||||
$constraint = new UniqueEntity(array(
|
||||
'message' => 'myMessage',
|
||||
'fields' => array('name'),
|
||||
'em' => self::EM_NAME,
|
||||
'entityClass' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity',
|
||||
));
|
||||
|
||||
$entity = new Person(1, 'Foo');
|
||||
$this->validator->validate($entity, $constraint);
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ class UniqueEntity extends Constraint
|
||||
public $message = 'This value is already used.';
|
||||
public $service = 'doctrine.orm.validator.unique';
|
||||
public $em = null;
|
||||
public $entityClass = null;
|
||||
public $repositoryMethod = 'findBy';
|
||||
public $fields = array();
|
||||
public $errorPath = null;
|
||||
|
@ -99,7 +99,21 @@ class UniqueEntityValidator extends ConstraintValidator
|
||||
}
|
||||
}
|
||||
|
||||
$repository = $em->getRepository(get_class($entity));
|
||||
if (null !== $constraint->entityClass) {
|
||||
/* Retrieve repository from given entity name.
|
||||
* We ensure the retrieved repository can handle the entity
|
||||
* by checking the entity is the same, or subclass of the supported entity.
|
||||
*/
|
||||
$repository = $em->getRepository($constraint->entityClass);
|
||||
$supportedClass = $repository->getClassName();
|
||||
|
||||
if (!$entity instanceof $supportedClass) {
|
||||
throw new ConstraintDefinitionException(sprintf('The "%s" entity repository does not support the "%s" entity. The entity should be an instance of or extend "%s".', $constraint->entityClass, $class->getName(), $supportedClass));
|
||||
}
|
||||
} else {
|
||||
$repository = $em->getRepository(get_class($entity));
|
||||
}
|
||||
|
||||
$result = $repository->{$constraint->repositoryMethod}($criteria);
|
||||
|
||||
if ($result instanceof \IteratorAggregate) {
|
||||
|
Reference in New Issue
Block a user