Reputation: 399
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()
;
}
Upvotes: 9
Views: 17952
Reputation: 51
public function deleteAllEmployees(){
$query = $this->createQueryBuilder('e')
->delete()
->getQuery()
->execute();
return $query;
}
Now, inside Controller
call this function with the entity manager object like em->deleteAllEmployees()
. Don't forget to flush()
.
Upvotes: 4
Reputation: 2466
Deleting from a QueryBuilder is completely defeating the purpose of having an ORM.
You should never consider your database rows, but only your entity objects. The ORM is responsible to map your entities to (and from) your table rows.
So you should, instead, get a collection of the entities you need to delete and delete them one for one.
Upvotes: -3
Reputation: 4089
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
Upvotes: 3
Reputation: 699
return $qb->delete(EntityB::class, "b")
->from(BaseUser::class, 'a')
->where('b.id = :survey')
->setParameter('survey', $surveyId)
->getQuery()
->execute()
;
Upvotes: -1
Reputation: 699
try to add b as params for $qb->delete(YourEntityForB::class,'b')
Upvotes: 0