[DoctrineMongoDBBundle] added support for multiple document managers
This commit is contained in:
parent
cf64d2cfe7
commit
ff34f7d281
@ -91,7 +91,7 @@
|
||||
|
||||
<!-- validator -->
|
||||
<service id="doctrine_odm.mongodb.validator.unique" class="%doctrine_odm.mongodb.validator.unique.class%">
|
||||
<argument type="service" id="doctrine.odm.mongodb.document_manager" />
|
||||
<argument type="service" id="service_container" />
|
||||
</service>
|
||||
|
||||
</services>
|
||||
|
@ -22,7 +22,8 @@ class DoctrineMongoDBUniqueValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
$this->classMetadata = $this->getClassMetadata();
|
||||
$this->repository = $this->getDocumentRepository();
|
||||
$this->dm = $this->getDocumentManager($this->classMetadata, $this->repository);
|
||||
$this->validator = new DoctrineMongoDBUniqueValidator($this->dm);
|
||||
$container = $this->getContainer();
|
||||
$this->validator = new DoctrineMongoDBUniqueValidator($container);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
@ -87,7 +88,18 @@ class DoctrineMongoDBUniqueValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
protected function getDocumentManager(ClassMetadata $classMetadata, DocumentRepository $repository)
|
||||
private function getContainer()
|
||||
{
|
||||
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
|
||||
|
||||
$container->expects($this->once())
|
||||
->method('get')
|
||||
->will($this->returnValue($this->dm));
|
||||
|
||||
return $container;
|
||||
}
|
||||
|
||||
private function getDocumentManager(ClassMetadata $classMetadata, DocumentRepository $repository)
|
||||
{
|
||||
$dm = $this->getMockBuilder('Doctrine\ODM\MongoDB\DocumentManager')
|
||||
->disableOriginalConstructor()
|
||||
|
@ -22,6 +22,7 @@ class DoctrineMongoDBUnique extends Constraint
|
||||
{
|
||||
public $message = 'The value for {{ property }} already exists.';
|
||||
public $path;
|
||||
public $documentManager;
|
||||
|
||||
public function defaultOption()
|
||||
{
|
||||
@ -42,4 +43,14 @@ class DoctrineMongoDBUnique extends Constraint
|
||||
{
|
||||
return Constraint::CLASS_CONSTRAINT;
|
||||
}
|
||||
|
||||
public function getDocumentManagerId()
|
||||
{
|
||||
$id = 'doctrine.odm.mongodb.document_manager';
|
||||
if (null !== $this->documentManager) {
|
||||
$id = sprintf('doctrine.odm.mongodb.%s_document_manager', $this->documentManager);
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ namespace Symfony\Bundle\DoctrineMongoDBBundle\Validator\Constraints;
|
||||
use Doctrine\ODM\MongoDB\DocumentManager;
|
||||
use Doctrine\ODM\MongoDB\Proxy\Proxy;
|
||||
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
@ -25,11 +26,11 @@ use Symfony\Component\Validator\ConstraintValidator;
|
||||
class DoctrineMongoDBUniqueValidator extends ConstraintValidator
|
||||
{
|
||||
|
||||
protected $dm;
|
||||
private $container;
|
||||
|
||||
public function __construct(DocumentManager $dm)
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->dm = $dm;
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,16 +41,17 @@ class DoctrineMongoDBUniqueValidator extends ConstraintValidator
|
||||
public function isValid($document, Constraint $constraint)
|
||||
{
|
||||
$class = get_class($document);
|
||||
$metadata = $this->dm->getClassMetadata($class);
|
||||
$dm = $this->getDocumentManager($constraint);
|
||||
$metadata = $dm->getClassMetadata($class);
|
||||
|
||||
if ($metadata->isEmbeddedDocument) {
|
||||
throw new \InvalidArgumentException(sprintf("Document '%s' is an embedded document, and cannot be validated", $class));
|
||||
}
|
||||
|
||||
$query = $this->getQueryArray($metadata, $document, $constraint->path);
|
||||
$query = $this->getQueryArray($metadata, $document, $constraint->path);
|
||||
|
||||
// check if document exists in mongodb
|
||||
if (null === ($doc = $this->dm->getRepository($class)->findOneBy($query))) {
|
||||
if (null === ($doc = $dm->getRepository($class)->findOneBy($query))) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -112,13 +114,13 @@ class DoctrineMongoDBUniqueValidator extends ConstraintValidator
|
||||
* @param string $field
|
||||
* @return string
|
||||
*/
|
||||
protected function getFieldNameFromPropertyPath($field)
|
||||
private function getFieldNameFromPropertyPath($field)
|
||||
{
|
||||
$pieces = explode('.', $field);
|
||||
return $pieces[0];
|
||||
}
|
||||
|
||||
protected function getFieldValueRecursively($fieldName, $value)
|
||||
private function getFieldValueRecursively($fieldName, $value)
|
||||
{
|
||||
$pieces = explode('.', $fieldName);
|
||||
unset($pieces[0]);
|
||||
@ -128,4 +130,9 @@ class DoctrineMongoDBUniqueValidator extends ConstraintValidator
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function getDocumentManager(DoctrineMongoDBUnique $constraint)
|
||||
{
|
||||
return $this->container->get($constraint->getDocumentManagerId());
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user