Roman  Kliuchko
Roman Kliuchko

Reputation: 499

Doctrine2 join doesn't works with delete

In my Repository class I've created method applyCriteria():

protected function applyCriteria(QueryBuilder $qb, array $criteria)
{
    $qb->leftJoin('co.file', 'file');

    $qb->andWhere($qb->expr()->eq('file.someField', $criteria['someField']));

    return $qb;
}

I use it in two methods:

public function findByCriteria(array $criteria = [])
{
    $qb = $this->applyCriteria($this->createQueryBuilder('co'), $criteria);

    return $qb->getQuery()->getResult();
}

public function deleteByCriteria(array $criteria = [])
{
    $qb = $this->applyCriteria($this->createQueryBuilder('co'), $criteria);

    $qb
        ->delete()
        ->getQuery()
        ->execute()
    ;
}

So, findByCriteria() works fine, but calling deleteByCriteria() throws Doctrine Exception with message Error: 'file' is not defined. What may cause the problem?

Upvotes: 0

Views: 240

Answers (1)

Roman  Kliuchko
Roman Kliuchko

Reputation: 499

I've found out. If anybody will need the same: Doctrine QueryBuilder delete with joins

The problem is that DQL really do not works with joins in delete queries. So we should use EntityManager and remove entities one-by-one or create another spikes.

Upvotes: 1

Related Questions