Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: symfony/symfony
base: 4bde2aac41
...
head fork: symfony/symfony
compare: 9f157a1616
  • 4 commits
  • 4 files changed
  • 3 commit comments
  • 2 contributors
Commits on Jul 19, 2012
@docteurklein docteurklein Add a customRepository option to the uniqueEntity validator 2a6c222
@docteurklein docteurklein [Doctrine] [Bridge] fix repositoryMethod test db9ea09
Commits on Jul 20, 2012
@docteurklein docteurklein update CHANGELOG 4eb54a0
@fabpot fabpot merged branch docteurklein/unique-entity-validator-with-custom-reposi…
…tory-method (PR #4979)

Commits
-------

4eb54a0 update CHANGELOG
db9ea09 [Doctrine] [Bridge] fix repositoryMethod test
2a6c222 Add a customRepository option to the uniqueEntity validator

Discussion
----------

[Doctrine] [Bridge] Add a "repositoryMethod" option to the uniqueEntity validator

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: ~
Todo: ~
License of the code: MIT
Documentation PR: ~

This allows to configure the repository method used to verify uniqueness of entity.
Before, it was always using `findBy`.

---------------------------------------------------------------------------

by fabpot at 2012-07-20T05:35:28Z

Can you add a note in the CHANGELOG?

---------------------------------------------------------------------------

by docteurklein at 2012-07-20T07:17:08Z

@fabpot done.
9f157a1
View
1  src/Symfony/Bridge/Doctrine/CHANGELOG.md
@@ -9,3 +9,4 @@ CHANGELOG
* DoctrineOrmTypeGuesser now guesses "collection" for array Doctrine type
* DoctrineType now caches its choice lists in order to improve performance
* DoctrineType now uses ManagerRegistry::getManagerForClass() if the option "em" is not set
+ * UniqueEntity validation constraint now accepts a "repositoryMethod" option that will be used to check for uniqueness instead of the default "findBy"
View
71 src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueValidatorTest.php
@@ -47,6 +47,50 @@ protected function createRegistryMock($entityManagerName, $em)
return $registry;
}
+ protected function createRepositoryMock()
+ {
+ $repository = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectRepository')
+ ->setMethods(array('findByCustom', 'find', 'findAll', 'findOneBy', 'findBy', 'getClassName'))
+ ->getMock()
+ ;
+
+ return $repository;
+ }
+
+ protected function createEntityManagerMock($repositoryMock)
+ {
+ $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
+ ->getMock()
+ ;
+ $em->expects($this->any())
+ ->method('getRepository')
+ ->will($this->returnValue($repositoryMock))
+ ;
+
+ $classMetadata = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadata');
+ $classMetadata
+ ->expects($this->any())
+ ->method('hasField')
+ ->will($this->returnValue(true))
+ ;
+ $refl = $this->getMockBuilder('Doctrine\Common\Reflection\StaticReflectionProperty')
+ ->disableOriginalConstructor()
+ ->getMock()
+ ;
+ $refl
+ ->expects($this->any())
+ ->method('getValue')
+ ->will($this->returnValue(true))
+ ;
+ $classMetadata->reflFields = array('name' => $refl);
+ $em->expects($this->any())
+ ->method('getClassMetadata')
+ ->will($this->returnValue($classMetadata))
+ ;
+
+ return $em;
+ }
+
protected function createMetadataFactoryMock($metadata)
{
$metadataFactory = $this->getMock('Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface');
@@ -69,7 +113,7 @@ protected function createValidatorFactory($uniqueValidator)
return $validatorFactory;
}
- public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null)
+ public function createValidator($entityManagerName, $em, $validateClass = null, $uniqueFields = null, $errorPath = null, $repositoryMethod = 'findBy')
{
if (!$validateClass) {
$validateClass = 'Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIdentEntity';
@@ -83,7 +127,13 @@ public function createValidator($entityManagerName, $em, $validateClass = null,
$uniqueValidator = new UniqueEntityValidator($registry);
$metadata = new ClassMetadata($validateClass);
- $metadata->addConstraint(new UniqueEntity(array('fields' => $uniqueFields, 'em' => $entityManagerName, 'errorPath' => $errorPath)));
+ $constraint = new UniqueEntity(array(
+ 'fields' => $uniqueFields,
+ 'em' => $entityManagerName,
+ 'errorPath' => $errorPath,
+ 'repositoryMethod' => $repositoryMethod
+ ));
+ $metadata->addConstraint($constraint);
$metadataFactory = $this->createMetadataFactoryMock($metadata);
$validatorFactory = $this->createValidatorFactory($uniqueValidator);
@@ -194,6 +244,23 @@ public function testValidateUniquenessAfterConsideringMultipleQueryResults()
$this->assertEquals(1, $violationsList->count(), 'Violation found on entity with conflicting entity existing in the database.');
}
+ public function testValidateUniquenessUsingCustomRepositoryMethod()
+ {
+ $entityManagerName = 'foo';
+ $repository = $this->createRepositoryMock();
+ $repository->expects($this->once())
+ ->method('findByCustom')
+ ->will($this->returnValue(array()))
+ ;
+ $em = $this->createEntityManagerMock($repository);
+ $validator = $this->createValidator($entityManagerName, $em, null, array(), null, 'findByCustom');
+
+ $entity1 = new SingleIdentEntity(1, 'foo');
+
+ $violationsList = $validator->validate($entity1);
+ $this->assertEquals(0, $violationsList->count(), 'Violation is using custom repository method.');
+ }
+
/**
* @group GH-1635
*/
View
1  src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntity.php
@@ -24,6 +24,7 @@ class UniqueEntity extends Constraint
public $message = 'This value is already used.';
public $service = 'doctrine.orm.validator.unique';
public $em = null;
+ public $repositoryMethod = 'findBy';
public $fields = array();
public $errorPath = null;
View
2  src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php
@@ -100,7 +100,7 @@ public function validate($entity, Constraint $constraint)
}
$repository = $em->getRepository($className);
- $result = $repository->findBy($criteria);
+ $result = $repository->{$constraint->repositoryMethod}($criteria);
/* If the result is a MongoCursor, it must be advanced to the first
* element. Rewinding should have no ill effect if $result is another

Showing you all comments on commits in this comparison.

@webmozart
Collaborator

StaticReflectionProperty exists only in Doctrine Common >= 2.3. This test fails on my machine with Doctrine Common 2.2.2.

@webmozart
Collaborator

Solution for now: #5052

@docteurklein

oh thanks!

Something went wrong with that request. Please try again.