SoftDeleteableWalker.php 4.2 KB

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