MultiTableDeleteExecutor.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Gedmo\SoftDeleteable\Query\TreeWalker\Exec;
  3. use Doctrine\ORM\Query\Exec\MultiTableDeleteExecutor as BaseMultiTableDeleteExecutor;
  4. use Doctrine\ORM\Query\AST\Node;
  5. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  6. use Doctrine\DBAL\Platforms\AbstractPlatform;
  7. /**
  8. * This class is used when a DELETE DQL query is called for entities
  9. * that are part of an inheritance tree
  10. *
  11. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Query.TreeWalker.Exec
  14. * @subpackage MultiTableDeleteExecutor
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class MultiTableDeleteExecutor extends BaseMultiTableDeleteExecutor
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function __construct(Node $AST, $sqlWalker, ClassMetadataInfo $meta, AbstractPlatform $platform, array $config)
  24. {
  25. parent::__construct($AST, $sqlWalker);
  26. $reflProp = new \ReflectionProperty(get_class($this), '_sqlStatements');
  27. $reflProp->setAccessible(true);
  28. $sqlStatements = $reflProp->getValue($this);
  29. foreach ($sqlStatements as $index => $stmt) {
  30. $matches = array();
  31. preg_match('/DELETE FROM (\w+) .+/', $stmt, $matches);
  32. if (isset($matches[1]) && $meta->getQuotedTableName($platform) === $matches[1]) {
  33. $sqlStatements[$index] = str_replace('DELETE FROM', 'UPDATE', $stmt);;
  34. $sqlStatements[$index] = str_replace('WHERE', 'SET '.$config['fieldName'].' = "'.date('Y-m-d H:i:s').'" WHERE', $sqlStatements[$index]);
  35. } else {
  36. // We have to avoid the removal of registers of child entities of a SoftDeleteable entity
  37. unset($sqlStatements[$index]);
  38. }
  39. }
  40. $reflProp->setValue($this, $sqlStatements);
  41. }
  42. }