AbstractTreeRepository.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace Gedmo\Tree\Entity\Repository;
  3. use Doctrine\ORM\EntityRepository,
  4. Doctrine\ORM\EntityManager,
  5. Doctrine\ORM\Mapping\ClassMetadata,
  6. Gedmo\Tool\Wrapper\EntityWrapper,
  7. Gedmo\Tree\RepositoryUtils,
  8. Gedmo\Tree\RepositoryUtilsInterface,
  9. Gedmo\Tree\RepositoryInterface,
  10. Gedmo\Exception\InvalidArgumentException;
  11. abstract class AbstractTreeRepository extends EntityRepository implements RepositoryInterface
  12. {
  13. /**
  14. * Tree listener on event manager
  15. *
  16. * @var AbstractTreeListener
  17. */
  18. protected $listener = null;
  19. /**
  20. * Repository utils
  21. */
  22. protected $repoUtils = null;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function __construct(EntityManager $em, ClassMetadata $class)
  27. {
  28. parent::__construct($em, $class);
  29. $treeListener = null;
  30. foreach ($em->getEventManager()->getListeners() as $listeners) {
  31. foreach ($listeners as $listener) {
  32. if ($listener instanceof \Gedmo\Tree\TreeListener) {
  33. $treeListener = $listener;
  34. break;
  35. }
  36. }
  37. if ($treeListener) {
  38. break;
  39. }
  40. }
  41. if (is_null($treeListener)) {
  42. throw new \Gedmo\Exception\InvalidMappingException('Tree listener was not found on your entity manager, it must be hooked into the event manager');
  43. }
  44. $this->listener = $treeListener;
  45. if (!$this->validate()) {
  46. throw new \Gedmo\Exception\InvalidMappingException('This repository cannot be used for tree type: ' . $treeListener->getStrategy($em, $class->name)->getName());
  47. }
  48. $this->repoUtils = new RepositoryUtils($this->_em, $this->getClassMetadata(), $this->listener, $this);
  49. }
  50. /**
  51. * Sets the RepositoryUtilsInterface instance
  52. *
  53. * @param \Gedmo\Tree\RepositoryUtilsInterface $repoUtils
  54. *
  55. * @return $this
  56. */
  57. public function setRepoUtils(RepositoryUtilsInterface $repoUtils)
  58. {
  59. $this->repoUtils = $repoUtils;
  60. return $this;
  61. }
  62. /**
  63. * Returns the RepositoryUtilsInterface instance
  64. *
  65. * @return \Gedmo\Tree\RepositoryUtilsInterface|null
  66. */
  67. public function getRepoUtils()
  68. {
  69. return $this->repoUtils;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function childCount($node = null, $direct = false)
  75. {
  76. $meta = $this->getClassMetadata();
  77. if (is_object($node)) {
  78. if (!($node instanceof $meta->name)) {
  79. throw new InvalidArgumentException("Node is not related to this repository");
  80. }
  81. $wrapped = new EntityWrapper($node, $this->_em);
  82. if (!$wrapped->hasValidIdentifier()) {
  83. throw new InvalidArgumentException("Node is not managed by UnitOfWork");
  84. }
  85. }
  86. $qb = $this->getChildrenQueryBuilder($node, $direct);
  87. // We need to remove the ORDER BY DQL part since some vendors could throw an error
  88. // in count queries
  89. $dqlParts = $qb->getDQLParts();
  90. // We need to check first if there's an ORDER BY DQL part, because resetDQLPart doesn't
  91. // check if its internal array has an "orderby" index
  92. if (isset($dqlParts['orderby'])) {
  93. $qb->resetDQLPart('orderby');
  94. }
  95. $aliases = $qb->getRootAliases();
  96. $alias = $aliases[0];
  97. $qb->select('COUNT('.$alias.')');
  98. return (int) $qb->getQuery()->getSingleScalarResult();
  99. }
  100. /**
  101. * @see \Gedmo\Tree\RepositoryUtilsInterface::childrenHierarchy
  102. */
  103. public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
  104. {
  105. return $this->repoUtils->childrenHierarchy($node, $direct, $options, $includeNode);
  106. }
  107. /**
  108. * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTree
  109. */
  110. public function buildTree(array $nodes, array $options = array())
  111. {
  112. return $this->repoUtils->buildTree($nodes, $options);
  113. }
  114. /**
  115. * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTreeArray
  116. */
  117. public function buildTreeArray(array $nodes)
  118. {
  119. return $this->repoUtils->buildTreeArray($nodes);
  120. }
  121. /**
  122. * Checks if current repository is right
  123. * for currently used tree strategy
  124. *
  125. * @return bool
  126. */
  127. abstract protected function validate();
  128. /**
  129. * Get all root nodes query builder
  130. *
  131. * @param string - Sort by field
  132. * @param string - Sort direction ("asc" or "desc")
  133. *
  134. * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
  135. */
  136. abstract public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc');
  137. /**
  138. * Get all root nodes query
  139. *
  140. * @param string - Sort by field
  141. * @param string - Sort direction ("asc" or "desc")
  142. *
  143. * @return \Doctrine\ORM\Query - Query object
  144. */
  145. abstract public function getRootNodesQuery($sortByField = null, $direction = 'asc');
  146. /**
  147. * Returns a QueryBuilder configured to return an array of nodes suitable for buildTree method
  148. *
  149. * @param object $node - Root node
  150. * @param bool $direct - Obtain direct children?
  151. * @param array $options - Options
  152. * @param boolean $includeNode - Include node in results?
  153. *
  154. * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
  155. */
  156. abstract public function getNodesHierarchyQueryBuilder($node = null, $direct = false, array $options = array(), $includeNode = false);
  157. /**
  158. * Returns a Query configured to return an array of nodes suitable for buildTree method
  159. *
  160. * @param object $node - Root node
  161. * @param bool $direct - Obtain direct children?
  162. * @param array $options - Options
  163. * @param boolean $includeNode - Include node in results?
  164. *
  165. * @return \Doctrine\ORM\Query - Query object
  166. */
  167. abstract public function getNodesHierarchyQuery($node = null, $direct = false, array $options = array(), $includeNode = false);
  168. /**
  169. * Get list of children followed by given $node. This returns a QueryBuilder object
  170. *
  171. * @param object $node - if null, all tree nodes will be taken
  172. * @param boolean $direct - true to take only direct children
  173. * @param string $sortByField - field name to sort by
  174. * @param string $direction - sort direction : "ASC" or "DESC"
  175. * @param bool $includeNode - Include the root node in results?
  176. *
  177. * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
  178. */
  179. abstract public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
  180. /**
  181. * Get list of children followed by given $node. This returns a Query
  182. *
  183. * @param object $node - if null, all tree nodes will be taken
  184. * @param boolean $direct - true to take only direct children
  185. * @param string $sortByField - field name to sort by
  186. * @param string $direction - sort direction : "ASC" or "DESC"
  187. * @param bool $includeNode - Include the root node in results?
  188. *
  189. * @return \Doctrine\ORM\Query - Query object
  190. */
  191. abstract public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
  192. }