MaterializedPathRepository.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * @return Doctrine\ORM\QueryBuilder
  25. */
  26. public function getTreeQueryBuilder()
  27. {
  28. return $this->getChildrenQueryBuilder();
  29. }
  30. /**
  31. * Get tree query
  32. *
  33. * @return Doctrine\ORM\Query
  34. */
  35. public function getTreeQuery()
  36. {
  37. return $this->getTreeQueryBuilder()->getQuery();
  38. }
  39. /**
  40. * Get tree
  41. *
  42. * @return array
  43. */
  44. public function getTree()
  45. {
  46. return $this->getTreeQuery()->execute();
  47. }
  48. /**
  49. * Get all root nodes query builder
  50. *
  51. * @return Doctrine\ORM\QueryBuilder
  52. */
  53. public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc')
  54. {
  55. return $this->getChildrenQueryBuilder(null, true, $sortByField, $direction);
  56. }
  57. /**
  58. * Get all root nodes query
  59. *
  60. * @return Doctrine\ORM\Query
  61. */
  62. public function getRootNodesQuery($sortByField = null, $direction = 'asc')
  63. {
  64. return $this->getRootNodesQueryBuilder($sortByField, $direction)->getQuery();
  65. }
  66. /**
  67. * Get all root nodes
  68. *
  69. * @return array
  70. */
  71. public function getRootNodes($sortByField = null, $direction = 'asc')
  72. {
  73. return $this->getRootNodesQuery($sortByField, $direction)->execute();
  74. }
  75. /**
  76. * Get children from node
  77. *
  78. * @return Doctrine\ORM\QueryBuilder
  79. */
  80. public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'asc')
  81. {
  82. $meta = $this->getClassMetadata();
  83. $config = $this->listener->getConfiguration($this->_em, $meta->name);
  84. $separator = addcslashes($config['path_separator'], '%');
  85. $alias = 'materialized_path_entity';
  86. $path = $config['path'];
  87. $qb = $this->_em->createQueryBuilder($meta->name)
  88. ->select($alias)
  89. ->from($meta->name, $alias);
  90. if (is_object($node) && $node instanceof $meta->name) {
  91. $node = new EntityWrapper($node, $this->_em);
  92. $nodePath = $node->getPropertyValue($path);
  93. $expr = $qb->expr()->andx()->add(
  94. $qb->expr()->like($alias.'.'.$path, $qb->expr()->literal($nodePath.'%'))
  95. );
  96. $expr->add($qb->expr()->neq($alias.'.'.$path, $qb->expr()->literal($nodePath)));
  97. if ($direct) {
  98. $expr->add(
  99. $qb->expr()->not(
  100. $qb->expr()->like($alias.'.'.$path, $qb->expr()->literal($nodePath.'%'.$separator.'%'.$separator))
  101. ));
  102. }
  103. $qb->where('('.$expr.')');
  104. } else if ($direct) {
  105. $expr = $qb->expr()->not(
  106. $qb->expr()->like($alias.'.'.$path, $qb->expr()->literal('%'.$separator.'%'.$separator.'%'))
  107. );
  108. $qb->where('('.$expr.')');
  109. }
  110. $orderByField = is_null($sortByField) ? $alias.'.'.$config['path'] : $alias.'.'.$sortByField;
  111. $orderByDir = $direction === 'asc' ? 'asc' : 'desc';
  112. $qb->orderBy($orderByField, $orderByDir);
  113. return $qb;
  114. }
  115. /**
  116. * Get children query
  117. *
  118. * @return Doctrine\ORM\Query
  119. */
  120. public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'asc')
  121. {
  122. return $this->getChildrenQueryBuilder($node, $direct, $sortByField, $direction)->getQuery();
  123. }
  124. /**
  125. * Get children
  126. *
  127. * @return array
  128. */
  129. public function getChildren($node = null, $direct = false, $sortByField = null, $direction = 'asc')
  130. {
  131. return $this->getChildrenQuery($node, $direct, $sortByField, $direction)->execute();
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function getNodesHierarchy($node, $direct, array $config, array $options = array())
  137. {
  138. return $this->getChildrenQuery()->getArrayResult();
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. protected function validate()
  144. {
  145. return $this->listener->getStrategy($this->_em, $this->getClassMetadata()->name)->getName() === Strategy::MATERIALIZED_PATH;
  146. }
  147. }