MaterializedPathRepository.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Gedmo\Tree\Document\MongoDB\Repository;
  3. use Gedmo\Exception\InvalidArgumentException,
  4. Gedmo\Tree\Strategy,
  5. Gedmo\Tree\Strategy\ODM\MongoDB\MaterializedPath;
  6. /**
  7. * The MaterializedPathRepository has some useful functions
  8. * to interact with MaterializedPath tree. Repository uses
  9. * the strategy used by listener
  10. *
  11. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Tree.Document.MongoDB.Repository
  14. * @subpackage MaterializedPathRepository
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class MaterializedPathRepository extends AbstractTreeRepository
  19. {
  20. /**
  21. * Get all root nodes query builder
  22. *
  23. * @return Doctrine\ODM\MongoDB\QueryBuilder
  24. */
  25. public function getRootNodesQueryBuilder()
  26. {
  27. $meta = $this->getClassMetadata();
  28. $config = $this->listener->getConfiguration($this->dm, $meta->name);
  29. $separator = preg_quote($config['path_separator']);
  30. return $this->dm->createQueryBuilder()
  31. ->find($meta->name)
  32. ->field($config['path'])->equals(new \MongoRegex(sprintf('/^[^%s]+%s{1}$/u',
  33. $separator,
  34. $separator)))
  35. ->sort($config['path'], 'asc');
  36. }
  37. /**
  38. * Get all root nodes query
  39. *
  40. * @return Doctrine\ODM\MongoDB\Query\Query
  41. */
  42. public function getRootNodesQuery()
  43. {
  44. return $this->getRootNodesQueryBuilder()->getQuery();
  45. }
  46. /**
  47. * Get all root nodes
  48. *
  49. * @return Doctrine\ODM\MongoDB\Cursor
  50. */
  51. public function getRootNodes()
  52. {
  53. return $this->getRootNodesQuery()->execute();
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function validates()
  59. {
  60. return $this->listener->getStrategy($this->dm, $this->getClassMetadata()->name)->getName() === Strategy::MATERIALIZED_PATH;
  61. }
  62. }