Skip to content

Commit

Permalink
[DoctrineMongoDBBundle] added support for multiple document managers
Browse files Browse the repository at this point in the history
  • Loading branch information
avalanche123 authored and fabpot committed Jan 26, 2011
1 parent cf64d2c commit ff34f7d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
Expand Up @@ -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>
Expand Down
Expand Up @@ -22,7 +22,8 @@ public function setUp()
$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()
Expand Down Expand Up @@ -87,7 +88,18 @@ public function getFieldTypesFieldsPathsValuesAndQueries()
);
}

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()
Expand Down
Expand Up @@ -22,6 +22,7 @@ class DoctrineMongoDBUnique extends Constraint
{
public $message = 'The value for {{ property }} already exists.';
public $path;
public $documentManager;

public function defaultOption()
{
Expand All @@ -42,4 +43,14 @@ public function targets()
{
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;
}
}
Expand Up @@ -14,6 +14,7 @@
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;

Expand All @@ -25,11 +26,11 @@
class DoctrineMongoDBUniqueValidator extends ConstraintValidator
{

protected $dm;
private $container;

public function __construct(DocumentManager $dm)
public function __construct(ContainerInterface $container)
{
$this->dm = $dm;
$this->container = $container;
}

/**
Expand All @@ -40,16 +41,17 @@ public function __construct(DocumentManager $dm)
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;
}

Expand Down Expand Up @@ -112,13 +114,13 @@ protected function getQueryArray(ClassMetadata $metadata, $document, $path)
* @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]);
Expand All @@ -128,4 +130,9 @@ protected function getFieldValueRecursively($fieldName, $value)
return $value;
}

private function getDocumentManager(DoctrineMongoDBUnique $constraint)
{
return $this->container->get($constraint->getDocumentManagerId());
}

}

0 comments on commit ff34f7d

Please sign in to comment.