AbstractTreeRepository.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. $aliases = $qb->getRootAliases();
  88. $alias = $aliases[0];
  89. $qb->select('COUNT('.$alias.')');
  90. $result = $qb->getQuery()->getScalarResult();
  91. return (int) $result[0][1];
  92. }
  93. /**
  94. * @see \Gedmo\Tree\RepositoryUtilsInterface::childrenHierarchy
  95. */
  96. public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
  97. {
  98. return $this->repoUtils->childrenHierarchy($node, $direct, $options, $includeNode);
  99. }
  100. /**
  101. * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTree
  102. */
  103. public function buildTree(array $nodes, array $options = array())
  104. {
  105. return $this->repoUtils->buildTree($nodes, $options);
  106. }
  107. /**
  108. * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTreeArray
  109. */
  110. public function buildTreeArray(array $nodes)
  111. {
  112. return $this->repoUtils->buildTreeArray($nodes);
  113. }
  114. /**
  115. * Checks if current repository is right
  116. * for currently used tree strategy
  117. *
  118. * @return bool
  119. */
  120. abstract protected function validate();
  121. /**
  122. * Get all root nodes query builder
  123. *
  124. * @param string - Sort by field
  125. * @param string - Sort direction ("asc" or "desc")
  126. *
  127. * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
  128. */
  129. abstract public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc');
  130. /**
  131. * Get all root nodes query
  132. *
  133. * @param string - Sort by field
  134. * @param string - Sort direction ("asc" or "desc")
  135. *
  136. * @return \Doctrine\ORM\Query - Query object
  137. */
  138. abstract public function getRootNodesQuery($sortByField = null, $direction = 'asc');
  139. /**
  140. * Returns a QueryBuilder configured to return an array of nodes suitable for buildTree method
  141. *
  142. * @param object $node - Root node
  143. * @param bool $direct - Obtain direct children?
  144. * @param array $config - Metadata configuration
  145. * @param array $options - Options
  146. * @param boolean $includeNode - Include node in results?
  147. *
  148. * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
  149. */
  150. abstract public function getNodesHierarchyQueryBuilder($node = null, $direct, array $config, array $options = array(), $includeNode = false);
  151. /**
  152. * Returns a Query configured to return an array of nodes suitable for buildTree method
  153. *
  154. * @param object $node - Root node
  155. * @param bool $direct - Obtain direct children?
  156. * @param array $config - Metadata configuration
  157. * @param array $options - Options
  158. * @param boolean $includeNode - Include node in results?
  159. *
  160. * @return \Doctrine\ORM\Query - Query object
  161. */
  162. abstract public function getNodesHierarchyQuery($node = null, $direct, array $config, array $options = array(), $includeNode = false);
  163. /**
  164. * Get list of children followed by given $node. This returns a QueryBuilder object
  165. *
  166. * @param object $node - if null, all tree nodes will be taken
  167. * @param boolean $direct - true to take only direct children
  168. * @param string $sortByField - field name to sort by
  169. * @param string $direction - sort direction : "ASC" or "DESC"
  170. * @param bool $includeNode - Include the root node in results?
  171. *
  172. * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
  173. */
  174. abstract public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
  175. /**
  176. * Get list of children followed by given $node. This returns a Query
  177. *
  178. * @param object $node - if null, all tree nodes will be taken
  179. * @param boolean $direct - true to take only direct children
  180. * @param string $sortByField - field name to sort by
  181. * @param string $direction - sort direction : "ASC" or "DESC"
  182. * @param bool $includeNode - Include the root node in results?
  183. *
  184. * @return \Doctrine\ORM\Query - Query object
  185. */
  186. abstract public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
  187. }