Skip to content

Commit bf8ed0a

Browse files
bug #26831 [Bridge/Doctrine] count(): Parameter must be an array or an object that implements Countable (gpenverne)
This PR was merged into the 2.7 branch. Discussion ---------- [Bridge/Doctrine] count(): Parameter must be an array or an object that implements Countable | Q | A | ------------- | --- | Branch? | master | | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Php7.2 will throw a warning on count(null) [http://php.net/manual/en/migration72.incompatible.php](http://php.net/manual/en/migration72.incompatible.php) Error: ``` count(): Parameter must be an array or an object that implements Countable ``` when no result returned on validating unique constraint For example, on an entity with annotation uniqueEntity: ``` @UniqueEntity( fields={"email"}, repositoryMethod="findMemberWithPasswordFromEmail", ) ``` And in repository, a method ``findMemberWithPasswordFromEmail`` which return null if no entity found (``getOneOrNullResult``) Commits ------- 715373f [Bridge/Doctrine] fix count() notice on PHP 7.2
2 parents 778d47f + 715373f commit bf8ed0a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

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

+28
Original file line numberDiff line numberDiff line change
@@ -517,4 +517,32 @@ public function testEntityManagerNullObject()
517517

518518
$this->validator->validate($entity, $constraint);
519519
}
520+
521+
public function testValidateUniquenessOnNullResult()
522+
{
523+
$repository = $this->createRepositoryMock();
524+
$repository
525+
->method('find')
526+
->will($this->returnValue(null))
527+
;
528+
529+
$this->em = $this->createEntityManagerMock($repository);
530+
$this->registry = $this->createRegistryMock($this->em);
531+
$this->validator = $this->createValidator();
532+
$this->validator->initialize($this->context);
533+
534+
$constraint = new UniqueEntity(array(
535+
'message' => 'myMessage',
536+
'fields' => array('name'),
537+
'em' => self::EM_NAME,
538+
));
539+
540+
$entity = new SingleIntIdEntity(1, null);
541+
542+
$this->em->persist($entity);
543+
$this->em->flush();
544+
545+
$this->validator->validate($entity, $constraint);
546+
$this->assertNoViolation();
547+
}
520548
}

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,23 @@ public function validate($entity, Constraint $constraint)
133133
*/
134134
if ($result instanceof \Iterator) {
135135
$result->rewind();
136-
} elseif (is_array($result)) {
136+
if ($result instanceof \Countable && 1 < \count($result)) {
137+
$result = array($result->current(), $result->current());
138+
} else {
139+
$result = $result->current();
140+
$result = null === $result ? array() : array($result);
141+
}
142+
} elseif (\is_array($result)) {
137143
reset($result);
144+
} else {
145+
$result = null === $result ? array() : array($result);
138146
}
139147

140148
/* If no entity matched the query criteria or a single entity matched,
141149
* which is the same as the entity being validated, the criteria is
142150
* unique.
143151
*/
144-
if (0 === count($result) || (1 === count($result) && $entity === ($result instanceof \Iterator ? $result->current() : current($result)))) {
152+
if (!$result || (1 === \count($result) && current($result) === $entity)) {
145153
return;
146154
}
147155

0 commit comments

Comments
 (0)