SoftDeleteableWalker.php 4.6 KB

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