Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Query Builder delete all entries from Table

i'm having some trouble deleting all records found by id using the query builder I tried like the bellow code but i'm always getting this error:

 [Semantical Error] line 0, col 53 near 'b.id = :surv': Error: 'b' is not defined. 

The Method:

public function deleteUsers($surveyId) {
    $qb = $this->getEntityManager()->createQueryBuilder();

    return $qb
        ->delete()
        ->from(BaseUser::class, 'a')
        ->leftJoin('a.survey', 'b')
        ->where('b.id = :survey')
        ->setParameter('survey', $surveyId)
        ->getQuery()
        ->execute()
        ;
}
like image 471
L.S Avatar asked Oct 03 '16 18:10

L.S


1 Answers

You simply cannot use joins in a delete statement with Doctrine. One chance could be to get the ids which should be deleted of your joined results first and then do a simple 'delete where ids in'. This will work.

Please see this question/answer as well: Doctrine QueryBuilder delete with joins

like image 58
LBA Avatar answered Oct 15 '22 14:10

LBA