MaterializedPath.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class MaterializedPath extends AbstractMaterializedPath
  14. {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public function removeNode($om, $meta, $config, $node)
  19. {
  20. $uow = $om->getUnitOfWork();
  21. $pathProp = $meta->getReflectionProperty($config['path']);
  22. $pathProp->setAccessible(true);
  23. $path = addcslashes($pathProp->getValue($node), '%');
  24. // Remove node's children
  25. $qb = $om->createQueryBuilder();
  26. $qb->select('e')
  27. ->from($config['useObjectClass'], 'e')
  28. ->where($qb->expr()->like('e.'.$config['path'], $qb->expr()->literal($path.'%')));
  29. $results = $qb->getQuery()
  30. ->execute();
  31. foreach ($results as $node) {
  32. $uow->scheduleForDelete($node);
  33. }
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getChildren($om, $meta, $config, $path)
  39. {
  40. $path = addcslashes($path, '%');
  41. $qb = $om->createQueryBuilder($config['useObjectClass']);
  42. $qb->select('e')
  43. ->from($config['useObjectClass'], 'e')
  44. ->where($qb->expr()->like('e.'.$config['path'], $qb->expr()->literal($path.'%')))
  45. ->andWhere('e.'.$config['path'].' != :path')
  46. ->orderBy('e.'.$config['path'], 'asc'); // This may save some calls to updateNode
  47. $qb->setParameter('path', $path);
  48. return $qb->getQuery()
  49. ->execute();
  50. }
  51. }