Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 SoftDeleteable not working on QueryBuilder Delete

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

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

like image 502
HMagdy Avatar asked Jul 15 '13 14:07

HMagdy


1 Answers

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

like image 183
Ken Hannel Avatar answered Oct 27 '22 13:10

Ken Hannel