Symfony2 SoftDeleteable not working on QueryBuilder Delete

隐身守侯 提交于 2019-11-30 22:08:19

If you use DQL then you have to use a Query Hint. This should do the trick:

$query = $qb->getQuery()

$query->setHint(
    \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
    'Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker'
);

$result = $query->getResult();

Update:

The docs mention that you have to use a Query Hint but don't provide an example so I pulled the usage from their tests.

Docs: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/softdeleteable.md

Test Usage: https://github.com/l3pp4rd/DoctrineExtensions/blob/master/tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php

my old solution after previous answer by @Ken Hannel is:

Edit: /vendor/doctrine/orm/lib/Doctrine/ORM/Query/SqlWalker.php

Replace walkDeleteClause function as the following:

 public function walkDeleteClause(AST\DeleteClause $deleteClause)
    {
        $class = $this->em->getClassMetadata($deleteClause->abstractSchemaName);
        $tableName = $class->getTableName();
        $sql = 'DELETE FROM ' . $this->quoteStrategy->getTableName($class, $this->platform);
        $this->setSQLTableAlias($tableName, $tableName, $deleteClause->aliasIdentificationVariable);
        $this->rootAliases[] = $deleteClause->aliasIdentificationVariable;
        //check if SoftDeleteableListener is attached 
        foreach ($this->em->getEventManager()->getListeners() as $eventName => $listeners) {
            foreach ($listeners as $listener) {
                if ($listener instanceof \Gedmo\SoftDeleteable\SoftDeleteableListener) {
                    $date = date('Y-m-d H:i:s');
                $sql = 'UPDATE ' . $this->quoteStrategy->getTableName($class, $this->platform) . " SET deletedAt = ' " . $date . " ' ";
                }
            }
        }
        return $sql;
    }

but really but I think Ken Hannel way is more professional and up to standard.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!