MaterializedPathRepository.php 4.8 KB

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