Skip to content

Instantly share code, notes, and snippets.

@janmarek
Created October 10, 2012 17:43
Show Gist options
  • Save janmarek/3867165 to your computer and use it in GitHub Desktop.
Save janmarek/3867165 to your computer and use it in GitHub Desktop.
DoctrineQueryBuilderBuilder
/**
* @author Jan Marek
*
* @method QueryBuilderBuilder andWhere() andWhere(\mixed $where)
* @method QueryBuilderBuilder orderBy() orderBy(\string $sort, \string $order = null)
* @method QueryBuilderBuilder from() from(\string $from, \string $alias, \string $indexBy = null)
* @method QueryBuilderBuilder select() select(\string $select = null)
* @method QueryBuilderBuilder addSelect() addSelect(\string $select = null)
* @method QueryBuilderBuilder setMaxResults() setMaxResults(\int $maxResults)
* @method QueryBuilderBuilder setFirstResult() setFirstResult(\int $firstResults)
* @method QueryBuilderBuilder setParameter() setParameter(\string $key, \mixed $value, \string $type = null)
*/
class QueryBuilderBuilder implements \IteratorAggregate
{
/**
* @var \Doctrine\ORM\QueryBuilder
*/
private $qb;
public function __construct(\Doctrine\ORM\QueryBuilder $qb)
{
$this->qb = $qb;
}
public function __call($name, $args)
{
$ret = call_user_func_array(array($this->qb, $name), $args);
return $ret === $this->qb ? $this : $ret;
}
public function getIterator()
{
return new \ArrayIterator($this->qb->getQuery()->getResult());
}
/**
* @return array
*/
public function getResult()
{
return $this->qb->getQuery()->getResult();
}
public function getSingleResult()
{
try {
return $this->qb->getQuery()->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return NULL;
}
}
public function count()
{
$paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($this->qb);
return $paginator->count();
}
/**
* @param int $limit
* @param int $offset
* @return QueryBuilderBuilder
*/
public function limit($limit, $offset = 0)
{
$this->qb->setMaxResults($limit);
$this->qb->setFirstResult($offset);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment