8000
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents c9927be + 690b185 commit 9c89adeCopy full SHA for 9c89ade
Tests/Fixtures/Employee.php
@@ -0,0 +1,19 @@
1
+<?php
2
+
3
+/*
4
+ * This file is part of the Symfony package.
5
+ *
6
+ * (c) Fabien Potencier <fabien@symfony.com>
7
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
12
+namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13
14
+use Doctrine\ORM\Mapping\Entity;
15
16
+/** @Entity */
17
+class Employee extends Person
18
+{
19
+}
Tests/Fixtures/Person.php
@@ -0,0 +1,45 @@
+use Doctrine\ORM\Mapping\DiscriminatorColumn;
+use Doctrine\ORM\Mapping\DiscriminatorMap;
+use Doctrine\ORM\Mapping\Id;
+use Doctrine\ORM\Mapping\Column;
+use Doctrine\ORM\Mapping\InheritanceType;
20
21
+/**
22
+ * @Entity
23
+ * @InheritanceType("SINGLE_TABLE")
24
+ * @DiscriminatorColumn(name="discr", type="string")
25
+ * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
26
27
+class Person
28
29
+ /** @Id @Column(type="integer") */
30
+ protected $id;
31
32
+ /** @Column(type="string") */
33
+ public $name;
34
35
+ public function __construct($id, $name)
36
+ {
37
+ $this->id = $id;
38
+ $this->name = $name;
39
+ }
40
41
+ public function __toString()
42
43
+ return (string) $this->name;
44
45
Tests/Validator/Constraints/UniqueEntityValidatorTest.php
@@ -16,6 +16,8 @@
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 @@ private function createSchema(ObjectManager $em)
134
136
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity'),
135
137
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
138
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity2'),
139
+ $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\Person'),
140
+ $em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\Employee'),
141
));
142
}
143
@@ -517,4 +521,54 @@ public function testEntityManagerNullObject()
517
521
518
522
$this->validator->validate($entity, $constraint);
519
523
524
525
+ public function testValidateInheritanceUniqueness()
526
527
+ $constraint = new UniqueEntity(array(
528
+ 'message' => 'myMessage',
529
+ 'fields' => array('name'),
530
+ 'em' => self::EM_NAME,
531
+ 'entityClass' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\Person',
532
+ ));
533
534
+ $entity1 = new Person(1, 'Foo');
535
+ $entity2 = new Employee(2, 'Foo');
536
537
+ $this->validator->validate($entity1, $constraint);
538
539
+ $this->assertNoViolation();
540
541
+ $this->em->persist($entity1);
542
+ $this->em->flush();
543
544
545
546
547
548
+ $this->validator->validate($entity2, $constraint);
549
550
+ $this->buildViolation('myMessage')
551
+ ->atPath('property.path.name')
552
+ ->setInvalidValue('Foo')
553
+ ->setCode('23bd9dbf-6b9b-41cd-a99e-4844bcf3077f')
554
+ ->setParameters(array('{{ value }}' => 'Foo'))
555
+ ->assertRaised();
556
557
558
+ /**
559
+ * @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
560
+ * @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".
561
562
+ public function testInvalidateRepositoryForInheritance()
563
564
565
566
567
568
+ 'entityClass' => 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity',
569
570
571
+ $entity = new Person(1, 'Foo');
572
+ $this->validator->validate($entity, $constraint);
573
520
574
Validator/Constraints/UniqueEntity.php
@@ -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;
Validator/Constraints/UniqueEntityValidator.php
@@ -99,7 +99,21 @@ public function validate($entity, Constraint $constraint)
99
100
101
102
- $repository = $em->getRepository(get_class($entity));
+ if (null !== $constraint->entityClass) {
103
+ /* Retrieve repository from given entity name.
104
+ * We ensure the retrieved repository can handle the entity
105
+ * by checking the entity is the same, or subclass of the supported entity.
106
107
+ $repository = $em->getRepository($constraint->entityClass);
108
+ $supportedClass = $repository->getClassName();
109
110
+ if (!$entity instanceof $supportedClass) {
111
+ 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));
112
113
+ } else {
114
+ $repository = $em->getRepository(get_class($entity));
115
116
117
$result = $repository->{$constraint->repositoryMethod}($criteria);
118
119
if ($result instanceof \IteratorAggregate) {