MaterializedPath.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Gedmo\Tree\Strategy\ORM;
  3. use Gedmo\Tree\Strategy\AbstractMaterializedPath;
  4. use Doctrine\Common\Persistence\ObjectManager;
  5. use Gedmo\Mapping\Event\AdapterInterface;
  6. /**
  7. * This strategy makes tree using materialized path strategy
  8. *
  9. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @package Gedmo.Tree.Strategy.ODM.MongoDB
  12. * @subpackage MaterializedPath
  13. * @link http://www.gediminasm.org
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class MaterializedPath extends AbstractMaterializedPath
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function removeNode($om, $meta, $config, $node)
  22. {
  23. $uow = $om->getUnitOfWork();
  24. $pathProp = $meta->getReflectionProperty($config['path']);
  25. $pathProp->setAccessible(true);
  26. $path = addcslashes($pathProp->getValue($node), '%');
  27. // Remove node's children
  28. $qb = $om->createQueryBuilder();
  29. $qb->select('e')
  30. ->from($meta->name, 'e')
  31. ->where($qb->expr()->like('e.'.$config['path'], $qb->expr()->literal($path.'%')));
  32. $results = $qb->getQuery()
  33. ->execute();
  34. foreach ($results as $node) {
  35. $uow->scheduleForDelete($node);
  36. }
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getChildren($om, $meta, $config, $path)
  42. {
  43. $path = addcslashes($path, '%');
  44. $qb = $om->createQueryBuilder($meta->name);
  45. $qb->select('e')
  46. ->from($meta->name, 'e')
  47. ->where($qb->expr()->like('e.'.$config['path'], $qb->expr()->literal($path.'%')))
  48. ->andWhere('e.'.$config['path'].' != :path')
  49. ->orderBy('e.'.$config['path'], 'asc'); // This may save some calls to updateNode
  50. $qb->setParameter('path', $path);
  51. return $qb->getQuery()
  52. ->execute();
  53. }
  54. }