MaterializedPathRepository.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace Gedmo\Tree\Entity\Repository;
  3. use Gedmo\Exception\InvalidArgumentException,
  4. Gedmo\Tree\Strategy,
  5. Gedmo\Tree\Strategy\ORM\MaterializedPath,
  6. Gedmo\Tool\Wrapper\EntityWrapper;
  7. /**
  8. * The MaterializedPathRepository has some useful functions
  9. * to interact with MaterializedPath tree. Repository uses
  10. * the strategy used by listener
  11. *
  12. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Tree.Entity.Repository
  15. * @subpackage MaterializedPathRepository
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class MaterializedPathRepository extends AbstractTreeRepository
  20. {
  21. /**
  22. * Get tree query builder
  23. *
  24. * @param object Root node
  25. *
  26. * @return Doctrine\ORM\QueryBuilder
  27. */
  28. public function getTreeQueryBuilder($rootNode = null)
  29. {
  30. return $this->getChildrenQueryBuilder($rootNode, false, null, 'asc', true);
  31. }
  32. /**
  33. * Get tree query
  34. *
  35. * @param object Root node
  36. *
  37. * @return Doctrine\ORM\Query
  38. */
  39. public function getTreeQuery($rootNode = null)
  40. {
  41. return $this->getTreeQueryBuilder($rootNode)->getQuery();
  42. }
  43. /**
  44. * Get tree
  45. *
  46. * @param object Root node
  47. *
  48. * @return array
  49. */
  50. public function getTree($rootNode = null)
  51. {
  52. return $this->getTreeQuery($rootNode)->execute();
  53. }
  54. /**
  55. * Get all root nodes query builder
  56. *
  57. * @return Doctrine\ORM\QueryBuilder
  58. */
  59. public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc')
  60. {
  61. return $this->getChildrenQueryBuilder(null, true, $sortByField, $direction);
  62. }
  63. /**
  64. * Get all root nodes query
  65. *
  66. * @return Doctrine\ORM\Query
  67. */
  68. public function getRootNodesQuery($sortByField = null, $direction = 'asc')
  69. {
  70. return $this->getRootNodesQueryBuilder($sortByField, $direction)->getQuery();
  71. }
  72. /**
  73. * Get all root nodes
  74. *
  75. * @return array
  76. */
  77. public function getRootNodes($sortByField = null, $direction = 'asc')
  78. {
  79. return $this->getRootNodesQuery($sortByField, $direction)->execute();
  80. }
  81. /**
  82. * Get children from node
  83. *
  84. * @return Doctrine\ORM\QueryBuilder
  85. */
  86. public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  87. {
  88. $meta = $this->getClassMetadata();
  89. $config = $this->listener->getConfiguration($this->_em, $meta->name);
  90. $separator = addcslashes($config['path_separator'], '%');
  91. $alias = 'materialized_path_entity';
  92. $path = $config['path'];
  93. $qb = $this->_em->createQueryBuilder($meta->name)
  94. ->select($alias)
  95. ->from($meta->name, $alias);
  96. $expr = '';
  97. if (is_object($node) && $node instanceof $meta->name) {
  98. $node = new EntityWrapper($node, $this->_em);
  99. $nodePath = $node->getPropertyValue($path);
  100. $expr = $qb->expr()->andx()->add(
  101. $qb->expr()->like($alias.'.'.$path, $qb->expr()->literal($nodePath.'%'))
  102. );
  103. if (!$includeNode) {
  104. $expr->add($qb->expr()->neq($alias.'.'.$path, $qb->expr()->literal($nodePath)));
  105. }
  106. if ($direct) {
  107. $expr->add(
  108. $qb->expr()->not(
  109. $qb->expr()->like($alias.'.'.$path, $qb->expr()->literal($nodePath.'%'.$separator.'%'.$separator))
  110. ));
  111. }
  112. } else if ($direct) {
  113. $expr = $qb->expr()->not(
  114. $qb->expr()->like($alias.'.'.$path, $qb->expr()->literal('%'.$separator.'%'.$separator.'%'))
  115. );
  116. }
  117. if ($expr) {
  118. $qb->where('('.$expr.')');
  119. }
  120. $orderByField = is_null($sortByField) ? $alias.'.'.$config['path'] : $alias.'.'.$sortByField;
  121. $orderByDir = $direction === 'asc' ? 'asc' : 'desc';
  122. $qb->orderBy($orderByField, $orderByDir);
  123. return $qb;
  124. }
  125. /**
  126. * Get children query
  127. *
  128. * @return Doctrine\ORM\Query
  129. */
  130. public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  131. {
  132. return $this->getChildrenQueryBuilder($node, $direct, $sortByField, $direction, $includeNode)->getQuery();
  133. }
  134. /**
  135. * Get children
  136. *
  137. * @return array
  138. */
  139. public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc', $includeNode = false)
  140. {
  141. return $this->getChildrenQuery($node, $direct, $sortByField, $direction, $includeNode)->execute();
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function getNodesHierarchyQueryBuilder($node, $direct, array $config, array $options = array())
  147. {
  148. $sortBy = array(
  149. 'field' => null,
  150. 'dir' => 'asc'
  151. );
  152. if (isset($options['childSort'])) {
  153. $sortBy = array_merge($sortBy, $options['childSort']);
  154. }
  155. return $this->getChildrenQueryBuilder($node, $direct, $sortBy['field'], $sortBy['dir'], true);
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function getNodesHierarchyQuery($node, $direct, array $config, array $options = array())
  161. {
  162. return $this->getNodesHierarchyQueryBuilder($node, $direct, $config, $options)->getQuery();
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function getNodesHierarchy($node, $direct, array $config, array $options = array())
  168. {
  169. return $this->getNodesHierarchyQuery($node, $direct, $config, $options)->getArrayResult();
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. protected function validate()
  175. {
  176. return $this->listener->getStrategy($this->_em, $this->getClassMetadata()->name)->getName() === Strategy::MATERIALIZED_PATH;
  177. }
  178. }