Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Bulk Delete with QueryBuilder

I'm trying to do a query for bulk deleting, but apparently it doesn't do the JOIN actions. Any thoughts?

$queryBuilder = $em
                ->createQueryBuilder()
                ->delete('Bundle:ClassA', 'a')
                ->join('a.classB', 'b')
                ->where('b = :parent')
                ->setParameter('parent', $parent);

      $queryBuilder->getQuery()->execute();

This returns:

"[Semantical Error] line 0, col 38 near 'b = :parent AND': Error: 'b' is not defined."

like image 253
ikleiman Avatar asked Nov 10 '13 21:11

ikleiman


2 Answers

DQL does not support joins in DELETE and UPDATEs, even if the underlaying database, like MySQL, supports it.

This is the quickiest workaround:

$results = $em
    ->createQueryBuilder()
    ->select('Bundle:ClassA', 'a')
    ->join('a.classB', 'b')
    ->where('b = :parent')
    ->setParameter('parent', $parent)
    ->getQuery()->getResult();

foreach ($results as $result){
    $em->remove($result);
}

$em->flush();
like image 200
Bob Avatar answered Sep 18 '22 03:09

Bob


try this :

$queryBuilder = $em
                ->createQueryBuilder()
                ->delete('Bundle:ClassA', 'a')
                ->innerJoin('a.classB', 'b')
                ->where($qb->expr()->eq('b', ':parent'))
                ->setParameter(':parent', $parent);

      $queryBuilder->getQuery()->execute();
like image 31
S.Thiongane Avatar answered Sep 18 '22 03:09

S.Thiongane