Skip to content

Commit

Permalink
bug #21431 [DoctrineBridge] always check for all fields to be mapped …
Browse files Browse the repository at this point in the history
…(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
  • Loading branch information
fabpot committed Jan 27, 2017
2 parents a35986f + 1e3421d commit bc391c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Expand Up @@ -244,6 +244,23 @@ public function testValidateUniquenessWithIgnoreNull()
->assertRaised();
}

/**
* @expectedException \Symfony\Component\Validator\Exception\ConstraintDefinitionException
*/
public function testAllConfiguredFieldsAreCheckedOfBeingMappedByDoctrineWithIgnoreNullEnabled()
{
$constraint = new UniqueEntity(array(
'message' => 'myMessage',
'fields' => array('name', 'name2'),
'em' => self::EM_NAME,
'ignoreNull' => true,
));

$entity1 = new SingleIntIdEntity(1, null);

$this->validator->validate($entity1, $constraint);
}

public function testValidateUniquenessWithValidCustomErrorPath()
{
$constraint = new UniqueEntity(array(
Expand Down
Expand Up @@ -85,12 +85,14 @@ public function validate($entity, Constraint $constraint)
throw new ConstraintDefinitionException(sprintf('The field "%s" is not mapped by Doctrine, so it cannot be validated for uniqueness.', $fieldName));
}

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

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

$criteria[$fieldName] = $fieldValue;

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

// skip validation if there are no criteria (this can happen when the
// "ignoreNull" option is enabled and fields to be checked are null
if (empty($criteria)) {
return;
}

$repository = $em->getRepository(get_class($entity));
$result = $repository->{$constraint->repositoryMethod}($criteria);

Expand Down

0 comments on commit bc391c1

Please sign in to comment.