8000 [Doctrine Bridge] Added a parameter ignoreNull on Unique entity to al… · symfony/symfony@f1c4b8b · GitHub
[go: up one dir, main page]

Skip to content

Commit f1c4b8b

Browse files
committed
[Doctrine Bridge] Added a parameter ignoreNull on Unique entity to allow a nullable value on field. Added Test
1 parent c20c1d1 commit f1c4b8b

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Id;
15+
use Doctrine\ORM\Mapping\Column;
16+
use Doctrine\ORM\Mapping\Entity;
17+
18+
/** @Entity */
19+
class DoubleIdentEntity
20+
{
21+
/** @Id @Column(type="integer") */
22+
protected $id;
23+
24+
/** @Column(type="string") */
25+
public $name;
26+
27+
/** @Column(type="string", nullable=true) */
28+
public $name2;
29+
30+
public function __construct($id, $name, $name2)
31+
{
32+
$this->id = $id;
33+
$this->name = $name;
34+
$this->name2 = $name2;
35+
}
36+
}

src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Bridge\Doctrine\Tests\DoctrineOrmTestCase;
1515
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity;
16+
use Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity;
1617
use Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity;
1718
use Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity;
1819
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
@@ -113,7 +114,7 @@ protected function createValidatorFactory($uniqueValidator)
113114
return $validatorFactory;
114115
}
115116

116-
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null, $repositoryMethod = 'findBy')
117+
public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null, $repositoryMethod = 'findBy', $ignoreNull = true)
117118
{
118119
if (!$validateClass) {
119120
$validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity';
@@ -131,7 +132,8 @@ public function createValidator($entityManagerName, $em, $validateClass = null,
131132
'fields' => $uniqueFields,
132133
'em' => $entityManagerName,
133134
'errorPath' => $errorPath,
134-
'repositoryMethod' => $repositoryMethod
135+
'repositoryMethod' => $repositoryMethod,
136+
'ignoreNull' => $ignoreNull
135137
));
136138
$metadata->addConstraint($constraint);
137139

@@ -146,6 +148,7 @@ private function createSchema($em)
146148
$schemaTool = new SchemaTool($em);
147149
$schemaTool->createSchema(array(
148150
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity'),
151+
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity'),
149152
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIdentEntity'),
150153
$em->getClassMetadata('Symfony\Bridge\Doctrine\Tests\Fixtures\AssociationEntity'),
151154
));
@@ -223,6 +226,35 @@ public function testValidateUniquenessWithNull()
223226
$this->assertEquals(0, $violationsList->count(), "No violations found on entity having a null value.");
224227
}
225228

229+
public function testValidateUniquenessWithIgnoreNull()
230+
{
231+
$entityManagerName = "foo";
232+
$validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\DoubleIdentEntity';
233+
$em = $this->createTestEntityManager();
234+
$this->createSchema($em);
235+
$validator = $this->createValidator($entityManagerName, $em, $validateClass, array('name', 'name2'), 'bar', 'findby', false);
236+
237+
$entity1 = new DoubleIdentEntity(1, 'Foo', null);
238+
$violationsList = $validator->validate($entity1);
239+
$this->assertEquals(0, $violationsList->count(), "No violations found on entity before it is saved to the database.");
240+
241+
$em->persist($entity1);
242+
$em->flush();
243+
244+
$violationsList = $validator->validate($entity1);
245+
$this->assertEquals(0, $violationsList->count(), "No violations found on entity after it was saved to the database.");
246+
247+
$entity2 = new DoubleIdentEntity(2, 'Foo', null);
248+
249+
$violationsList = $validator->validate($entity2);
250+
$this->assertEquals(1, $violationsList->count(), "Violation found on entity with conflicting entity existing in the database.");
251+
252+
$violation = $violationsList[0];
253+
$this->assertEquals('This value is already used.', $violation->getMessage());
254+
$this->assertEquals('bar', $violation->getPropertyPath());
255+
$this->assertEquals('Foo', $violation->getInvalidValue());
256+
}
257+
226258
public function testValidateUniquenessAfterConsideringMultipleQueryResults()
227259
{
228260
$entityManagerName = "foo";

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class UniqueEntity extends Constraint
2727
public $repositoryMethod = 'findBy';
2828
public $fields = array();
2929
public $errorPath = null;
30+
public $ignoreNull = true;
3031

3132
public function getRequiredOptions()
3233
{

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function validate($entity, Constraint $constraint)
7575

7676
$criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
7777

78-
if (null === $criteria[$fieldName]) {
78+
if ($constraint->ignoreNull && null === $criteria[$fieldName]) {
7979
return;
8080
}
8181

0 commit comments

Comments
 (0)
0