8000 bug #21431 [DoctrineBridge] always check for all fields to be mapped … · symfony/symfony@bc391c1 · GitHub
[go: up one dir, main page]

Skip to content

Commit bc391c1

Browse files
committed
bug #21431 [DoctrineBridge] always check for all fields to be mapped (xabbuh)
This PR was merged into the 2.7 branch. Discussion ---------- [DoctrineBridge] always check for all fields to be mapped | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16791 | License | MIT | Doc PR | Commits ------- 1e3421d always check for all fields to be mapped
2 parents a35986f + 1e3421d commit bc391c1

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,23 @@ public function testValidateUniquenessWithIgnoreNull()
244244
->assertRaised();
245245
}
246246

247+
/**
248+
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
249+
*/
250+
public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgnoreNullEnabled()
251+
{
252+
$constraint = new UniqueEntity(array(
253+
'message' => 'myMessage',
254+
'fields' => array('name', 'name2'),
255+
'em' => self::EM_NAME,
256+
'ignoreNull' => true,
257+
));
258+
259+
$entity1 = new SingleIntIdEntity(1, null);
260+
261+
$this->validator->validate($entity1, $constraint);
262+
}
263+
247264
public function testValidateUniquenessWithValidCustomErrorPath()
248265
{
249266
$constraint = new UniqueEntity(array(

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ public function validate($entity, Constraint $constraint)
8585
throw new ConstraintDefinitionException(sprintf('The field "%s" is not mapped by Doctrine, so it cannot be validated for uniqueness.', $fieldName));
8686
}
8787

88-
$criteria[$fieldName] = $class->reflFields[$fieldName]->getValue($entity);
88+
$fieldValue = $class->reflFields[$fieldName]->getValue($entity);
8989

90-
if ($constraint->ignoreNull && null === $criteria[$fieldName]) {
91-
return;
90+
if ($constraint->ignoreNull && null === $fieldValue) {
91+
continue;
9292
}
9393

94+
$criteria[$fieldName] = $fieldValue;
95+
9496
if (null !== $criteria[$fieldName] && $class->hasAssociation($fieldName)) {
9597
/* Ensure the Proxy is initialized before using reflection to
9698
* read its identifiers. This is necessary because the wrapped
@@ -100,6 +102,12 @@ public function validate($entity, Constraint $constraint)
100102
}
101103
}
102104

105+
// skip validation if there are no criteria (this can happen when the
106+
// "ignoreNull" option is enabled and fields to be checked are null
107+
if (empty($criteria)) {
108+
return;
109+
}
110+
103111
$repository = $em->getRepository(get_class($entity));
104112
$result = $repository->{$constraint->repositoryMethod}($criteria);
105113

0 commit comments

Comments
 (0)
0