SoftDeleteableWalker.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Gedmo\SoftDeleteable\Query\TreeWalker;
  3. use Doctrine\ORM\Query\SqlWalker;
  4. use Doctrine\ORM\Query\AST\DeleteStatement;
  5. use Doctrine\ORM\Query\AST\DeleteClause;
  6. use Doctrine\ORM\Query\AST\UpdateClause;
  7. use Doctrine\ORM\Query\AST\UpdateItem;
  8. use Doctrine\ORM\Query\Exec\SingleTableDeleteUpdateExecutor;
  9. use Doctrine\ORM\Query\Exec\MultiTableDeleteExecutor;
  10. use Doctrine\ORM\Query\AST\PathExpression;
  11. use Gedmo\SoftDeleteable\SoftDeleteableListener;
  12. /**
  13. * This SqlWalker is needed when you need to use a DELETE DQL query.
  14. * It will update the "deletedAt" field with the actual date, instead
  15. * of actually deleting it.
  16. *
  17. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  18. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  19. * @package Gedmo.Query.TreeWalker
  20. * @subpackage SoftDeleteableWalker
  21. * @link http://www.gediminasm.org
  22. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  23. */
  24. class SoftDeleteableWalker extends SqlWalker
  25. {
  26. protected $conn;
  27. protected $platform;
  28. protected $listener;
  29. protected $alias;
  30. protected $deletedAtField;
  31. protected $meta;
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function __construct($query, $parserResult, array $queryComponents)
  36. {
  37. parent::__construct($query, $parserResult, $queryComponents);
  38. $this->conn = $this->getConnection();
  39. $this->platform = $this->conn->getDatabasePlatform();
  40. $this->listener = $this->getSoftDeleteableListener();
  41. $this->extractComponents($queryComponents);
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function getExecutor($AST)
  47. {
  48. switch (true) {
  49. case ($AST instanceof DeleteStatement):
  50. $primaryClass = $this->getEntityManager()->getClassMetadata($AST->deleteClause->abstractSchemaName);
  51. return ($primaryClass->isInheritanceTypeJoined())
  52. ? new MultiTableDeleteExecutor($AST, $this)
  53. : new SingleTableDeleteUpdateExecutor($AST, $this);
  54. default:
  55. throw new \Gedmo\Exception\UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');
  56. }
  57. }
  58. /**
  59. * Change a DELETE clause for an UPDATE clause
  60. *
  61. * @param DeleteClause
  62. * @return string The SQL.
  63. */
  64. public function walkDeleteClause(DeleteClause $deleteClause)
  65. {
  66. $em = $this->getEntityManager();
  67. $class = $em->getClassMetadata($deleteClause->abstractSchemaName);
  68. $tableName = $class->getTableName();
  69. $this->setSQLTableAlias($tableName, $tableName, $deleteClause->aliasIdentificationVariable);
  70. $quotedTableName = $class->getQuotedTableName($this->platform);
  71. $quotedColumnName = $class->getQuotedColumnName($this->deletedAtField, $this->platform);
  72. $sql = 'UPDATE '.$quotedTableName.' SET '.$quotedColumnName.' = "'.date('Y-m-d H:i:s').'"';
  73. return $sql;
  74. }
  75. /**
  76. * Get the currently used SoftDeleteableListener
  77. *
  78. * @throws \Gedmo\Exception\RuntimeException - if listener is not found
  79. * @return SoftDeleteableListener
  80. */
  81. private function getSoftDeleteableListener()
  82. {
  83. if (is_null($this->listener)) {
  84. $em = $this->getEntityManager();
  85. foreach ($em->getEventManager()->getListeners() as $event => $listeners) {
  86. foreach ($listeners as $hash => $listener) {
  87. if ($listener instanceof SoftDeleteableListener) {
  88. $this->listener = $listener;
  89. break;
  90. }
  91. }
  92. if ($this->listener) {
  93. break;
  94. }
  95. }
  96. if (is_null($this->listener)) {
  97. throw new \Gedmo\Exception\RuntimeException('The SoftDeleteable listener could not be found.');
  98. }
  99. }
  100. return $this->listener;
  101. }
  102. /**
  103. * Search for components in the delete clause
  104. *
  105. * @param array $queryComponents
  106. * @return void
  107. */
  108. private function extractComponents(array $queryComponents)
  109. {
  110. $em = $this->getEntityManager();
  111. foreach ($queryComponents as $alias => $comp) {
  112. if (!isset($comp['metadata'])) {
  113. continue;
  114. }
  115. $meta = $comp['metadata'];
  116. $config = $this->listener->getConfiguration($em, $meta->name);
  117. if ($config && isset($config['softDeleteable']) && $config['softDeleteable']) {
  118. $this->deletedAtField = $config['fieldName'];
  119. $this->meta = $meta;
  120. }
  121. }
  122. }
  123. }