123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace Gedmo\Tree\Entity\Repository;
- use Doctrine\ORM\EntityRepository,
- Doctrine\ORM\EntityManager,
- Doctrine\ORM\Mapping\ClassMetadata,
- Gedmo\Tool\Wrapper\EntityWrapper,
- Gedmo\Tree\RepositoryUtils,
- Gedmo\Tree\RepositoryUtilsInterface,
- Gedmo\Tree\RepositoryInterface,
- Gedmo\Exception\InvalidArgumentException;
- abstract class AbstractTreeRepository extends EntityRepository implements RepositoryInterface
- {
- /**
- * Tree listener on event manager
- *
- * @var AbstractTreeListener
- */
- protected $listener = null;
- /**
- * Repository utils
- */
- protected $repoUtils = null;
- /**
- * {@inheritdoc}
- */
- public function __construct(EntityManager $em, ClassMetadata $class)
- {
- parent::__construct($em, $class);
- $treeListener = null;
- foreach ($em->getEventManager()->getListeners() as $listeners) {
- foreach ($listeners as $listener) {
- if ($listener instanceof \Gedmo\Tree\TreeListener) {
- $treeListener = $listener;
- break;
- }
- }
- if ($treeListener) {
- break;
- }
- }
- if (is_null($treeListener)) {
- throw new \Gedmo\Exception\InvalidMappingException('Tree listener was not found on your entity manager, it must be hooked into the event manager');
- }
- $this->listener = $treeListener;
- if (!$this->validate()) {
- throw new \Gedmo\Exception\InvalidMappingException('This repository cannot be used for tree type: ' . $treeListener->getStrategy($em, $class->name)->getName());
- }
- $this->repoUtils = new RepositoryUtils($this->_em, $this->getClassMetadata(), $this->listener, $this);
- }
- /**
- * Sets the RepositoryUtilsInterface instance
- *
- * @param \Gedmo\Tree\RepositoryUtilsInterface $repoUtils
- *
- * @return $this
- */
- public function setRepoUtils(RepositoryUtilsInterface $repoUtils)
- {
- $this->repoUtils = $repoUtils;
- return $this;
- }
- /**
- * Returns the RepositoryUtilsInterface instance
- *
- * @return \Gedmo\Tree\RepositoryUtilsInterface|null
- */
- public function getRepoUtils()
- {
- return $this->repoUtils;
- }
- /**
- * {@inheritDoc}
- */
- public function childCount($node = null, $direct = false)
- {
- $meta = $this->getClassMetadata();
- if (is_object($node)) {
- if (!($node instanceof $meta->name)) {
- throw new InvalidArgumentException("Node is not related to this repository");
- }
- $wrapped = new EntityWrapper($node, $this->_em);
- if (!$wrapped->hasValidIdentifier()) {
- throw new InvalidArgumentException("Node is not managed by UnitOfWork");
- }
- }
- $qb = $this->getChildrenQueryBuilder($node, $direct);
- $aliases = $qb->getRootAliases();
- $alias = $aliases[0];
- $qb->select('COUNT('.$alias.')');
- $result = $qb->getQuery()->getScalarResult();
- return (int) $result[0][1];
- }
- /**
- * @see \Gedmo\Tree\RepositoryUtilsInterface::childrenHierarchy
- */
- public function childrenHierarchy($node = null, $direct = false, array $options = array(), $includeNode = false)
- {
- return $this->repoUtils->childrenHierarchy($node, $direct, $options, $includeNode);
- }
- /**
- * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTree
- */
- public function buildTree(array $nodes, array $options = array())
- {
- return $this->repoUtils->buildTree($nodes, $options);
- }
- /**
- * @see \Gedmo\Tree\RepositoryUtilsInterface::buildTreeArray
- */
- public function buildTreeArray(array $nodes)
- {
- return $this->repoUtils->buildTreeArray($nodes);
- }
- /**
- * Checks if current repository is right
- * for currently used tree strategy
- *
- * @return bool
- */
- abstract protected function validate();
- /**
- * Get all root nodes query builder
- *
- * @param string - Sort by field
- * @param string - Sort direction ("asc" or "desc")
- *
- * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
- */
- abstract public function getRootNodesQueryBuilder($sortByField = null, $direction = 'asc');
- /**
- * Get all root nodes query
- *
- * @param string - Sort by field
- * @param string - Sort direction ("asc" or "desc")
- *
- * @return \Doctrine\ORM\Query - Query object
- */
- abstract public function getRootNodesQuery($sortByField = null, $direction = 'asc');
- /**
- * Returns a QueryBuilder configured to return an array of nodes suitable for buildTree method
- *
- * @param object $node - Root node
- * @param bool $direct - Obtain direct children?
- * @param array $config - Metadata configuration
- * @param array $options - Options
- * @param boolean $includeNode - Include node in results?
- *
- * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
- */
- abstract public function getNodesHierarchyQueryBuilder($node = null, $direct, array $config, array $options = array(), $includeNode = false);
- /**
- * Returns a Query configured to return an array of nodes suitable for buildTree method
- *
- * @param object $node - Root node
- * @param bool $direct - Obtain direct children?
- * @param array $config - Metadata configuration
- * @param array $options - Options
- * @param boolean $includeNode - Include node in results?
- *
- * @return \Doctrine\ORM\Query - Query object
- */
- abstract public function getNodesHierarchyQuery($node = null, $direct, array $config, array $options = array(), $includeNode = false);
- /**
- * Get list of children followed by given $node. This returns a QueryBuilder object
- *
- * @param object $node - if null, all tree nodes will be taken
- * @param boolean $direct - true to take only direct children
- * @param string $sortByField - field name to sort by
- * @param string $direction - sort direction : "ASC" or "DESC"
- * @param bool $includeNode - Include the root node in results?
- *
- * @return \Doctrine\ORM\QueryBuilder - QueryBuilder object
- */
- abstract public function getChildrenQueryBuilder($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
- /**
- * Get list of children followed by given $node. This returns a Query
- *
- * @param object $node - if null, all tree nodes will be taken
- * @param boolean $direct - true to take only direct children
- * @param string $sortByField - field name to sort by
- * @param string $direction - sort direction : "ASC" or "DESC"
- * @param bool $includeNode - Include the root node in results?
- *
- * @return \Doctrine\ORM\Query - Query object
- */
- abstract public function getChildrenQuery($node = null, $direct = false, $sortByField = null, $direction = 'ASC', $includeNode = false);
- }
|