Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
<?php
namespace Gedmo\SoftDeleteable\Query\TreeWalker\Exec;
use Doctrine\ORM\Query\Exec\MultiTableDeleteExecutor as BaseMultiTableDeleteExecutor;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* This class is used when a DELETE DQL query is called for entities
* that are part of an inheritance tree
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class MultiTableDeleteExecutor extends BaseMultiTableDeleteExecutor
{
/**
* {@inheritDoc}
*/
public function __construct(Node $AST, $sqlWalker, ClassMetadataInfo $meta, AbstractPlatform $platform, array $config)
{
parent::__construct($AST, $sqlWalker);
$reflProp = new \ReflectionProperty(get_class($this), '_sqlStatements');
$reflProp->setAccessible(true);
$sqlStatements = $reflProp->getValue($this);
foreach ($sqlStatements as $index => $stmt) {
$matches = array();
preg_match('/DELETE FROM (\w+) .+/', $stmt, $matches);
if (isset($matches[1]) && $meta->getQuotedTableName($platform) === $matches[1]) {
$sqlStatements[$index] = str_replace('DELETE FROM', 'UPDATE', $stmt);
$sqlStatements[$index] = str_replace(
'WHERE',
'SET '.$config['fieldName'].' = '.$platform->getCurrentTimestampSQL().' WHERE',
$sqlStatements[$index]
);
} else {
// We have to avoid the removal of registers of child entities of a SoftDeleteable entity
unset($sqlStatements[$index]);
}
}
$reflProp->setValue($this, $sqlStatements);
}
}

View File

@@ -0,0 +1,139 @@
<?php
namespace Gedmo\SoftDeleteable\Query\TreeWalker;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\DeleteStatement;
use Doctrine\ORM\Query\AST\DeleteClause;
use Doctrine\ORM\Query\Exec\SingleTableDeleteUpdateExecutor;
use Gedmo\SoftDeleteable\SoftDeleteableListener;
use Gedmo\SoftDeleteable\Query\TreeWalker\Exec\MultiTableDeleteExecutor;
/**
* This SqlWalker is needed when you need to use a DELETE DQL query.
* It will update the "deletedAt" field with the actual date, instead
* of actually deleting it.
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class SoftDeleteableWalker extends SqlWalker
{
protected $conn;
protected $platform;
protected $listener;
protected $configuration;
protected $alias;
protected $deletedAtField;
protected $meta;
/**
* {@inheritDoc}
*/
public function __construct($query, $parserResult, array $queryComponents)
{
parent::__construct($query, $parserResult, $queryComponents);
$this->conn = $this->getConnection();
$this->platform = $this->conn->getDatabasePlatform();
$this->listener = $this->getSoftDeleteableListener();
$this->extractComponents($queryComponents);
}
/**
* {@inheritDoc}
*/
public function getExecutor($AST)
{
switch (true) {
case ($AST instanceof DeleteStatement):
$primaryClass = $this->getEntityManager()->getClassMetadata($AST->deleteClause->abstractSchemaName);
return ($primaryClass->isInheritanceTypeJoined())
? new MultiTableDeleteExecutor($AST, $this, $this->meta, $this->platform, $this->configuration)
: new SingleTableDeleteUpdateExecutor($AST, $this);
default:
throw new \Gedmo\Exception\UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');
}
}
/**
* Change a DELETE clause for an UPDATE clause
*
* @param DeleteClause $deleteClause
*
* @return string The SQL.
*/
public function walkDeleteClause(DeleteClause $deleteClause)
{
$em = $this->getEntityManager();
$class = $em->getClassMetadata($deleteClause->abstractSchemaName);
$tableName = $class->getTableName();
$this->setSQLTableAlias($tableName, $tableName, $deleteClause->aliasIdentificationVariable);
$quotedTableName = $class->getQuotedTableName($this->platform);
$quotedColumnName = $class->getQuotedColumnName($this->deletedAtField, $this->platform);
$sql = 'UPDATE '.$quotedTableName.' SET '.$quotedColumnName.' = '.$this->platform->getCurrentTimestampSQL();
return $sql;
}
/**
* Get the currently used SoftDeleteableListener
*
* @throws \Gedmo\Exception\RuntimeException - if listener is not found
*
* @return SoftDeleteableListener
*/
private function getSoftDeleteableListener()
{
if (is_null($this->listener)) {
$em = $this->getEntityManager();
foreach ($em->getEventManager()->getListeners() as $event => $listeners) {
foreach ($listeners as $hash => $listener) {
if ($listener instanceof SoftDeleteableListener) {
$this->listener = $listener;
break;
}
}
if ($this->listener) {
break;
}
}
if (is_null($this->listener)) {
throw new \Gedmo\Exception\RuntimeException('The SoftDeleteable listener could not be found.');
}
}
return $this->listener;
}
/**
* Search for components in the delete clause
*
* @param array $queryComponents
*
* @return void
*/
private function extractComponents(array $queryComponents)
{
$em = $this->getEntityManager();
foreach ($queryComponents as $alias => $comp) {
if (!isset($comp['metadata'])) {
continue;
}
$meta = $comp['metadata'];
$config = $this->listener->getConfiguration($em, $meta->name);
if ($config && isset($config['softDeleteable']) && $config['softDeleteable']) {
$this->configuration = $config;
$this->deletedAtField = $config['fieldName'];
$this->meta = $meta;
}
}
}
}