php - Symfony2 SoftDeleteable 不适用于 QueryBuilder Delete

标签 php symfony doctrine-orm soft-delete

Softdelete 行为在通过实体管理器执行删除语句时工作正常,如下代码:

$entity = $this->em->getRepository('Users')->find(7);
$this->em->remove($entity);
$this->em->flush();

但是当通过 QueryBuilder 执行相同的功能时,硬删除将在数据库上执行

$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();

如何通过实体管理器或查询构建器在所有情况下允许软删除

最佳答案

如果您使用 DQL,则必须使用查询提示。这应该可以解决问题:

$query = $qb->getQuery()

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

$result = $query->getResult();

更新:

文档提到您必须使用查询提示但没有提供示例,因此我从他们的测试中提取了用法。

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

测试用法:https://github.com/l3pp4rd/DoctrineExtensions/blob/master/tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php

关于php - Symfony2 SoftDeleteable 不适用于 QueryBuilder Delete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17656983/

相关文章:

Symfony 2.1 Doctrine 过滤器(启用/禁用)

php - Symfony2 如何在 Controller 中使用 parameters.yml 作为变量

database - 来自现有数据库的 Symfony2 Doctrine2 映射(异常(exception))

php - Symfony 和 Doctrine DQL 查询生成器 : how to use multiple setParameters inside if condition checks?

php - Woocommerce 和 Opayo : Add a custom field to the data sent to the API

php - 在函数 pdo php 中传递参数

javascript - 静态资源(js,css ...)的 Symfony 2 路由

php - 如何设置外键id的id?

javascript - 从一段文本中找出javascript中的关键字

php - Laravel 中的 Guzzle 不断加载,不返回值