Skip to content

Commit

Permalink
doctrine-reader: add querybuilder support (#12)
Browse files Browse the repository at this point in the history
* doctrine-reader: add querybuilder support

* doctrinereader: make getQueryBuilder non-public
  • Loading branch information
glensc authored and ddeboer committed Apr 21, 2019
1 parent 0044de4 commit fe07221
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/DoctrineReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\Internal\Hydration\IterableResult;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Port\Reader\CountableReader;

/**
Expand All @@ -29,23 +30,43 @@ class DoctrineReader implements CountableReader
*/
protected $iterableResult;

/** @var QueryBuilder */
protected $queryBuilder;

/**
* @param ObjectManager $objectManager
* @param string $objectName e.g. YourBundle:YourEntity
* @param string $objectName e.g. YourBundle:YourEntity
*/
public function __construct(ObjectManager $objectManager, $objectName)
{
$this->objectManager = $objectManager;
$this->objectName = $objectName;
}

public function setQueryBuilder(QueryBuilder $queryBuilder)
{
$this->queryBuilder = $queryBuilder;

return $this;
}

protected function getQueryBuilder()
{
if ($this->queryBuilder === null) {
$this->queryBuilder = $this->objectManager->createQueryBuilder()
->from($this->objectName, 'o');
}

return clone $this->queryBuilder;
}

/**
* {@inheritdoc}
*/
public function getFields()
{
return $this->objectManager->getClassMetadata($this->objectName)
->getFieldNames();
->getFieldNames();
}

/**
Expand Down Expand Up @@ -86,11 +107,7 @@ public function valid()
public function rewind()
{
if (!$this->iterableResult) {

$query = $this->objectManager->createQueryBuilder()
->select('o')
->from($this->objectName, 'o')
->getQuery();
$query = $this->getQueryBuilder()->select('o')->getQuery();

$this->iterableResult = $query->iterate([], Query::HYDRATE_ARRAY);
}
Expand All @@ -103,10 +120,7 @@ public function rewind()
*/
public function count()
{
$query = $this->objectManager->createQueryBuilder()
->select('count(o)')
->from($this->objectName, 'o')
->getQuery();
$query = $this->getQueryBuilder()->select('count(o)')->getQuery();

return $query->getSingleScalarResult();
}
Expand Down

0 comments on commit fe07221

Please sign in to comment.