Symfony2 SoftDeleteable 不适用于 QueryBuilder 删除
Softdelete 行为在通过实体管理器执行删除语句时正常工作,如下代码:
Softdelete behavior works fine on execute delete statement via the entity manager as the following code:
$entity = $this->em->getRepository('Users')->find(7);
$this->em->remove($entity);
$this->em->flush();
但是当通过 QueryBuilder 执行相同的功能时,硬删除将在数据库上执行
but when execute the same functionality via QueryBuilder hard delete will execute on database
$qb = $this->em->createQueryBuilder();
$qb->delete('Users', 'p');
$qb->where($qb->expr()->eq('p.id', ':id'));
$qb->setParameters(array("id" => 7));
$result = $qb->getQuery()->getResult();
如何在所有情况下通过实体管理器或查询构建器允许软删除
How can I allow softdelete in all cases either via entity manager or query builder
推荐答案
如果您使用 DQL,那么您必须使用查询提示.这应该可以解决问题:
If you use DQL then you have to use a Query Hint. This should do the trick:
$query = $qb->getQuery()
$query->setHint(
DoctrineORMQuery::HINT_CUSTOM_OUTPUT_WALKER,
'GedmoSoftDeleteableQueryTreeWalkerSoftDeleteableWalker'
);
$result = $query->getResult();
更新:
文档提到您必须使用查询提示,但没有提供示例,因此我从他们的测试中提取了用法.
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.
文档:https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/softdeleteable.md
测试用法:https://github.com/l3pp4rd/DoctrineExtensions/blob/master/tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php
相关文章