8000 [DoctrineBridge] Add a way to select the repository used by the UniqueEntity validator by ogizanagi · Pull Request #15002 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Doctrine] [Bridge] Add a way to select the repository used by the Un…
…iqueEntity 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.
  • Loading branch information
maxime.steinhausser authored and ogizanagi committed Oct 7, 2016
commit 00d54597caac7ba2bd3b8e4e22faf634e2f6b75b
19 changes: 19 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/Employee.php
Original file line number Diff line number Diff line change
@@ -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 changes: 45 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Fixtures/Person.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Doctrine\Common\Persistence\ObjectManage 8000 r;
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;
Expand Down Expand Up @@ -134,6 +136,8 @@ private function createSchema(ObjectManager $em)
$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'),
));
}

Expand Down Expand Up @@ -517,4 +521,54 @@ public function testEntityManagerNullObject()

$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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,21 @@ public function validate($entity, Constraint $constraint)
}
}

$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->{ 4711 $constraint->repositoryMethod}($criteria);

if ($result instanceof \IteratorAggregate) {
Expand Down
0