Permalink
Browse files

Merge pull request #196 from dantleech/dm_create_query_builder

Added createQueryBuilder() method to the Document Repository
  • Loading branch information...
2 parents 6c2b95c + ca59fd1 commit d794314922606a63c8db321a0c5af0d8f5bd0f55 @dbu dbu committed Dec 9, 2012
@@ -276,4 +276,30 @@ public function getDocumentsByQuery(\PHPCR\Query\QueryInterface $query)
{
return $this->dm->getDocumentsByQuery($query, $this->className);
}
+
+ /**
+ * Create a QueryBuilder that is prepopulated for this repositories document
+ *
+ * The returned query builder will be prepopulated with the criteria
+ * required to search for this repositories document class.
+ *
+ * NOTE: When adding criteria to the query builder you should
+ * use ->andWhere(...) as ->where(...) will overwrite
+ * the class criteria.
+ *
+ * @return QueryBuilder
+ */
+ public function createQueryBuilder()
+ {
+ $qb = $this->dm->createQueryBuilder();
+ $qf = $qb->getQOMFactory();
+ $qb->from($qf->selector($this->class->nodeType));
+ $qb->andWhere(
+ $qf->comparison($qf->propertyValue('phpcr:class'),
+ Constants::JCR_OPERATOR_EQUAL_TO,
+ $qf->literal($this->className))
+ );
+
+ return $qb;
+ }
}
@@ -0,0 +1,36 @@
+<?php
+
+namespace Doctrine\Tests\ODM\PHPCR;
+
+use Doctrine\ODM\PHPCR\DocumentManager;
+use Doctrine\ODM\PHPCR\DocumentRepository;
+
+/**
+ * @group functional
+ */
+class DocumentRepositoryTest extends PHPCRFunctionalTestCase
+{
+ public function setUp()
+ {
+ $session = $this->getMock('PHPCR\SessionInterface');
+ $config = new \Doctrine\ODM\PHPCR\Configuration();
+ $this->dm = $this->createDocumentManager();
+ $this->metadata = new \Doctrine\ODM\PHPCR\Mapping\ClassMetadata('stdClass');
+ $this->metadata->setNodeType('test:node');
+ }
+
+ public function testCreateQueryBuilder()
+ {
+ $rep = new DocumentRepository($this->dm, $this->metadata);
+ $qb = $rep->createQueryBuilder();
+
+ $constraint = $qb->getConstraint();
+ $op1 = $constraint->getOperand1();
+ $op2 = $constraint->getOperand2();
+ $source = $qb->getSource();
+
+ $this->assertEquals('phpcr:class', $op1->getPropertyName());
+ $this->assertEquals('stdClass', $op2->getLiteralValue());
+ $this->assertEquals('test:node', $source->getNodeTypeName());
+ }
+}

0 comments on commit d794314

Please sign in to comment.