MaterializedPath.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Gedmo\Tree\Strategy\ODM\MongoDB;
  3. use Gedmo\Tree\Strategy\AbstractMaterializedPath;
  4. /**
  5. * This strategy makes tree using materialized path strategy
  6. *
  7. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. * @package Gedmo.Tree.Strategy.ODM.MongoDB
  10. * @subpackage MaterializedPath
  11. * @link http://www.gediminasm.org
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class MaterializedPath extends AbstractMaterializedPath
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function removeNode($om, $meta, $config, $node)
  20. {
  21. $uow = $om->getUnitOfWork();
  22. $pathProp = $meta->getReflectionProperty($config['path']);
  23. $pathProp->setAccessible(true);
  24. // Remove node's children
  25. $results = $om->createQueryBuilder()
  26. ->find($meta->name)
  27. ->field($config['path'])->equals(new \MongoRegex('/^'.preg_quote($pathProp->getValue($node)).'.?+/'))
  28. ->getQuery()
  29. ->execute();
  30. foreach ($results as $node) {
  31. $uow->scheduleForDelete($node);
  32. }
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getChildren($om, $meta, $config, $originalPath)
  38. {
  39. return $om->createQueryBuilder()
  40. ->find($meta->name)
  41. ->field($config['path'])->equals(new \MongoRegex('/^'.preg_quote($originalPath).'.+/'))
  42. ->sort($config['path'], 'asc') // This may save some calls to updateNode
  43. ->getQuery()
  44. ->execute();
  45. }
  46. }