Skip to content

Commit

Permalink
[tree][nested] made possibility to take query builder for tree operat…
Browse files Browse the repository at this point in the history
…ions, closes doctrine-extensions#142
  • Loading branch information
l3pp4rd committed Sep 9, 2011
1 parent 71543c3 commit e702c72
Showing 1 changed file with 128 additions and 46 deletions.
174 changes: 128 additions & 46 deletions lib/Gedmo/Tree/Entity/Repository/NestedTreeRepository.php
Expand Up @@ -23,20 +23,30 @@
class NestedTreeRepository extends AbstractTreeRepository class NestedTreeRepository extends AbstractTreeRepository
{ {
/** /**
* Get all root nodes query * Get all root nodes query builder
* *
* @return Query * @return Doctrine\ORM\QueryBuilder
*/ */
public function getRootNodesQuery() public function getRootNodesQueryBuilder()
{ {
$meta = $this->getClassMetadata(); $meta = $this->getClassMetadata();
$config = $this->listener->getConfiguration($this->_em, $meta->name); $config = $this->listener->getConfiguration($this->_em, $meta->name);
$qb = $this->_em->createQueryBuilder(); return $this->_em->createQueryBuilder()
$qb->select('node') ->select('node')
->from($config['useObjectClass'], 'node') ->from($config['useObjectClass'], 'node')
->where('node.' . $config['parent'] . " IS NULL") ->where('node.' . $config['parent'] . " IS NULL")
->orderBy('node.' . $config['left'], 'ASC'); ->orderBy('node.' . $config['left'], 'ASC')
return $qb->getQuery(); ;
}

/**
* Get all root nodes query
*
* @return Doctrine\ORM\Query
*/
public function getRootNodesQuery()
{
return $this->getRootNodesQueryBuilder()->getQuery();
} }


/** /**
Expand Down Expand Up @@ -100,13 +110,13 @@ public function __call($method, $args)
} }


/** /**
* Get the Tree path query by given $node * Get the Tree path query builder by given $node
* *
* @param object $node * @param object $node
* @throws InvalidArgumentException - if input is not valid * @throws InvalidArgumentException - if input is not valid
* @return Query * @return Doctrine\ORM\QueryBuilder
*/ */
public function getPathQuery($node) public function getPathQueryBuilder($node)
{ {
$meta = $this->getClassMetadata(); $meta = $this->getClassMetadata();
if (!$node instanceof $meta->name) { if (!$node instanceof $meta->name) {
Expand All @@ -122,16 +132,26 @@ public function getPathQuery($node)
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('node') $qb->select('node')
->from($config['useObjectClass'], 'node') ->from($config['useObjectClass'], 'node')
->where('node.' . $config['left'] . " <= :left") ->where('node.' . $config['left'] . " <= {$left}")
->andWhere('node.' . $config['right'] . " >= :right") ->andWhere('node.' . $config['right'] . " >= {$right}")
->orderBy('node.' . $config['left'], 'ASC'); ->orderBy('node.' . $config['left'], 'ASC')
;
if (isset($config['root'])) { if (isset($config['root'])) {
$rootId = $wrapped->getPropertyValue($config['root']); $rootId = $wrapped->getPropertyValue($config['root']);
$qb->andWhere("node.{$config['root']} = {$rootId}"); $qb->andWhere("node.{$config['root']} = {$rootId}");
} }
$q = $qb->getQuery(); return $qb;
$q->setParameters(compact('left', 'right')); }
return $q;
/**
* Get the Tree path query by given $node
*
* @param object $node
* @return Doctrine\ORM\Query
*/
public function getPathQuery($node)
{
return $this->getPathQueryBuilder($node)->getQuery();
} }


/** /**
Expand Down Expand Up @@ -200,23 +220,24 @@ public function childCount($node = null, $direct = false)
} }


/** /**
* Get tree children query followed by given $node * Get tree children query builder followed by given $node
* *
* @param object $node - if null, all tree nodes will be taken * @param object $node - if null, all tree nodes will be taken
* @param boolean $direct - true to take only direct children * @param boolean $direct - true to take only direct children
* @param string $sortByField - field name to sort by * @param string $sortByField - field name to sort by
* @param string $direction - sort direction : "ASC" or "DESC" * @param string $direction - sort direction : "ASC" or "DESC"
* @throws InvalidArgumentException - if input is not valid * @throws InvalidArgumentException - if input is not valid
* @return Query * @return Doctrine\ORM\QueryBuilder
*/ */
public function childrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC') public function childrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC')
{ {
$meta = $this->getClassMetadata(); $meta = $this->getClassMetadata();
$config = $this->listener->getConfiguration($this->_em, $meta->name); $config = $this->listener->getConfiguration($this->_em, $meta->name);


$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('node') $qb->select('node')
->from($config['useObjectClass'], 'node'); ->from($config['useObjectClass'], 'node')
;
if ($node !== null) { if ($node !== null) {
if ($node instanceof $meta->name) { if ($node instanceof $meta->name) {
$wrapped = new EntityWrapper($node, $this->_em); $wrapped = new EntityWrapper($node, $this->_em);
Expand All @@ -230,8 +251,10 @@ public function childrenQuery($node = null, $direct = false, $sortByField = null
$left = $wrapped->getPropertyValue($config['left']); $left = $wrapped->getPropertyValue($config['left']);
$right = $wrapped->getPropertyValue($config['right']); $right = $wrapped->getPropertyValue($config['right']);
if ($left && $right) { if ($left && $right) {
$qb->where('node.' . $config['right'] . " < {$right}") $qb
->andWhere('node.' . $config['left'] . " > {$left}"); ->where('node.' . $config['right'] . " < {$right}")
->andWhere('node.' . $config['left'] . " > {$left}")
;
} }
} }
if (isset($config['root'])) { if (isset($config['root'])) {
Expand All @@ -255,7 +278,21 @@ public function childrenQuery($node = null, $direct = false, $sortByField = null
throw new InvalidArgumentException("Invalid sort options specified: field - {$sortByField}, direction - {$direction}"); throw new InvalidArgumentException("Invalid sort options specified: field - {$sortByField}, direction - {$direction}");
} }
} }
return $qb->getQuery(); return $qb;
}

/**
* Get tree children query followed by given $node
*
* @param object $node - if null, all tree nodes will be taken
* @param boolean $direct - true to take only direct children
* @param string $sortByField - field name to sort by
* @param string $direction - sort direction : "ASC" or "DESC"
* @return Doctrine\ORM\Query
*/
public function childrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC')
{
return $this->childrenQueryBuilder($node, $direct, $sortByField, $direction)->getQuery();
} }


/** /**
Expand All @@ -274,15 +311,15 @@ public function children($node = null, $direct = false, $sortByField = null, $di
} }


/** /**
* Get tree leafs query * Get tree leafs query builder
* *
* @param object $root - root node in case of root tree is required * @param object $root - root node in case of root tree is required
* @param string $sortByField - field name to sort by * @param string $sortByField - field name to sort by
* @param string $direction - sort direction : "ASC" or "DESC" * @param string $direction - sort direction : "ASC" or "DESC"
* @throws InvalidArgumentException - if input is not valid * @throws InvalidArgumentException - if input is not valid
* @return Query * @return Doctrine\ORM\QueryBuilder
*/ */
public function getLeafsQuery($root = null, $sortByField = null, $direction = 'ASC') public function getLeafsQueryBuilder($root = null, $sortByField = null, $direction = 'ASC')
{ {
$meta = $this->getClassMetadata(); $meta = $this->getClassMetadata();
$config = $this->listener->getConfiguration($this->_em, $meta->name); $config = $this->listener->getConfiguration($this->_em, $meta->name);
Expand All @@ -296,7 +333,8 @@ public function getLeafsQuery($root = null, $sortByField = null, $direction = 'A
$qb = $this->_em->createQueryBuilder(); $qb = $this->_em->createQueryBuilder();
$qb->select('node') $qb->select('node')
->from($config['useObjectClass'], 'node') ->from($config['useObjectClass'], 'node')
->where('node.' . $config['right'] . ' = 1 + node.' . $config['left']); ->where('node.' . $config['right'] . ' = 1 + node.' . $config['left'])
;
if (isset($config['root'])) { if (isset($config['root'])) {
if ($root instanceof $meta->name) { if ($root instanceof $meta->name) {
$wrapped = new EntityWrapper($root, $this->_em); $wrapped = new EntityWrapper($root, $this->_em);
Expand All @@ -318,7 +356,20 @@ public function getLeafsQuery($root = null, $sortByField = null, $direction = 'A
throw new InvalidArgumentException("Invalid sort options specified: field - {$sortByField}, direction - {$direction}"); throw new InvalidArgumentException("Invalid sort options specified: field - {$sortByField}, direction - {$direction}");
} }
} }
return $qb->getQuery(); return $qb;
}

/**
* Get tree leafs query
*
* @param object $root - root node in case of root tree is required
* @param string $sortByField - field name to sort by
* @param string $direction - sort direction : "ASC" or "DESC"
* @return Doctrine\ORM\Query
*/
public function getLeafsQuery($root = null, $sortByField = null, $direction = 'ASC')
{
return $this->getLeafsQueryBuilder($root, $sortByField, $direction)->getQuery();
} }


/** /**
Expand All @@ -335,14 +386,14 @@ public function getLeafs($root = null, $sortByField = null, $direction = 'ASC')
} }


/** /**
* Get the query for next siblings of the given $node * Get the query builder for next siblings of the given $node
* *
* @param object $node * @param object $node
* @param bool $includeSelf - include the node itself * @param bool $includeSelf - include the node itself
* @throws \Gedmo\Exception\InvalidArgumentException - if input is invalid * @throws \Gedmo\Exception\InvalidArgumentException - if input is invalid
* @return Query * @return Doctrine\ORM\QueryBuilder
*/ */
public function getNextSiblingsQuery($node, $includeSelf = false) public function getNextSiblingsQueryBuilder($node, $includeSelf = false)
{ {
$meta = $this->getClassMetadata(); $meta = $this->getClassMetadata();
if (!$node instanceof $meta->name) { if (!$node instanceof $meta->name) {
Expand All @@ -362,17 +413,32 @@ public function getNextSiblingsQuery($node, $includeSelf = false)
$left = $wrapped->getPropertyValue($config['left']); $left = $wrapped->getPropertyValue($config['left']);
$sign = $includeSelf ? '>=' : '>'; $sign = $includeSelf ? '>=' : '>';


$dql = "SELECT node FROM {$config['useObjectClass']} node"; $qb = $this->_em->createQueryBuilder();
$qb->select('node')
->from($config['useObjectClass'], 'node')
->where("node.{$config['left']} {$sign} {$left}")
->orderBy("node.{$config['left']}", 'ASC')
;
if ($parent) { if ($parent) {
$wrappedParent = new EntityWrapper($parent, $this->_em); $wrappedParent = new EntityWrapper($parent, $this->_em);
$parentId = $wrappedParent->getIdentifier(); $parentId = $wrappedParent->getIdentifier();
$dql .= " WHERE node.{$config['parent']} = {$parentId}"; $qb->andWhere("node.{$config['parent']} = {$parentId}");
} else { } else {
$dql .= " WHERE node.{$config['parent']} IS NULL"; $qb->andWhere($qb->expr()->isNull('node.'.$config['parent']));
} }
$dql .= " AND node.{$config['left']} {$sign} {$left}"; return $qb;
$dql .= " ORDER BY node.{$config['left']} ASC"; }
return $this->_em->createQuery($dql);
/**
* Get the query for next siblings of the given $node
*
* @param object $node
* @param bool $includeSelf - include the node itself
* @return Doctrine\ORM\Query
*/
public function getNextSiblingsQuery($node, $includeSelf = false)
{
return $this->getNextSiblingsQueryBuilder($node, $includeSelf)->getQuery();
} }


/** /**
Expand All @@ -388,14 +454,14 @@ public function getNextSiblings($node, $includeSelf = false)
} }


/** /**
* Get query for previous siblings of the given $node * Get query builder for previous siblings of the given $node
* *
* @param object $node * @param object $node
* @param bool $includeSelf - include the node itself * @param bool $includeSelf - include the node itself
* @throws \Gedmo\Exception\InvalidArgumentException - if input is invalid * @throws \Gedmo\Exception\InvalidArgumentException - if input is invalid
* @return Query * @return Doctrine\ORM\QueryBuilder
*/ */
public function getPrevSiblingsQuery($node, $includeSelf = false) public function getPrevSiblingsQueryBuilder($node, $includeSelf = false)
{ {
$meta = $this->getClassMetadata(); $meta = $this->getClassMetadata();
if (!$node instanceof $meta->name) { if (!$node instanceof $meta->name) {
Expand All @@ -415,17 +481,33 @@ public function getPrevSiblingsQuery($node, $includeSelf = false)
$left = $wrapped->getPropertyValue($config['left']); $left = $wrapped->getPropertyValue($config['left']);
$sign = $includeSelf ? '<=' : '<'; $sign = $includeSelf ? '<=' : '<';


$dql = "SELECT node FROM {$config['useObjectClass']} node"; $qb = $this->_em->createQueryBuilder();
$qb->select('node')
->from($config['useObjectClass'], 'node')
->where("node.{$config['left']} {$sign} {$left}")
->orderBy("node.{$config['left']}", 'ASC')
;
if ($parent) { if ($parent) {
$wrappedParent = new EntityWrapper($parent, $this->_em); $wrappedParent = new EntityWrapper($parent, $this->_em);
$parentId = $wrappedParent->getIdentifier(); $parentId = $wrappedParent->getIdentifier();
$dql .= " WHERE node.{$config['parent']} = {$parentId}"; $qb->andWhere("node.{$config['parent']} = {$parentId}");
} else { } else {
$dql .= " WHERE node.{$config['parent']} IS NULL"; $qb->andWhere($qb->expr()->isNull('node.'.$config['parent']));
} }
$dql .= " AND node.{$config['left']} {$sign} {$left}"; return $qb;
$dql .= " ORDER BY node.{$config['left']} ASC"; }
return $this->_em->createQuery($dql);
/**
* Get query for previous siblings of the given $node
*
* @param object $node
* @param bool $includeSelf - include the node itself
* @throws \Gedmo\Exception\InvalidArgumentException - if input is invalid
* @return Doctrine\ORM\Query
*/
public function getPrevSiblingsQuery($node, $includeSelf = false)
{
return $this->getPrevSiblingsQueryBuilder($node, $includeSelf)->getQuery();
} }


/** /**
Expand Down

0 comments on commit e702c72

Please sign in to comment.