MaterializedPath.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 processScheduledInsertion($om, $node)
  20. {
  21. // TODO
  22. }
  23. /**
  24. * Remove node and its children
  25. *
  26. * @param ObjectManager $om
  27. * @param object $meta - Metadata
  28. * @param object $config - config
  29. * @param object $node - node to remove
  30. * @return void
  31. */
  32. public function removeNode($om, $meta, $config, $node)
  33. {
  34. $pathProp = $meta->getReflectionProperty($config['path']);
  35. $pathProp->setAccessible(true);
  36. // Remove node and its children
  37. $om->createQueryBuilder()
  38. ->remove($meta->name)
  39. ->field($config['path'])->equals(new \MongoRegex('/^'.preg_quote($pathProp->getValue($node)).'.?+/'))
  40. ->getQuery()
  41. ->execute();
  42. }
  43. /**
  44. * Returns children of the node with its original path
  45. *
  46. * @param ObjectManager $om
  47. * @param object $meta - Metadata
  48. * @param object $config - config
  49. * @param mixed $originalPath - original path of object
  50. * @return void
  51. */
  52. public function getChildren($om, $meta, $config, $originalPath)
  53. {
  54. return $om->createQueryBuilder()
  55. ->find($meta->name)
  56. ->field($config['path'])->equals(new \MongoRegex('/^'.$originalPath.'.+/'))
  57. ->sort($config['path'], 'asc') // This may save some calls to updateNode
  58. ->getQuery()
  59. ->execute();
  60. }
  61. }