Answer the question
In order to leave comments, you need to log in
What is the best way to generate a delete request in Doctrine?
You need to remove all conditional values from the associated table.
DELETE news_bad
FROM smi_ad_news_bad AS news_bad
INNER JOIN smi_ad_campaigns_news AS news ON news_bad.news_id = news.id
WHERE news.status != "active"
AND news_bad.dt < (now() - interval 1 month);
$this->em
->getRepository(NewsBad::class)
->createQueryBuilder('nb')
->delete(NewsBad::class, 'nb')
->join(News::class, 'n')
->andWhere('n.status != :status')
->andWhere('n.dt < :date')
->setParameters([
'date' => $date,
'status' => "active",
])
->getQuery()
->getSql()
$qb = $this->em->createQuery('
DELETE newsBad
FROM AppBundle\Entity\NewsBad newsBad
INNER JOIN AppBundle\Entity\News news ON newsBad.news_id = news.id
WHERE news.status != "active"
AND news_bad.dt < (now() - interval 1 month);
');
$qb->execute();
return $this->createQueryBuilder('nb')
->delete()
->where($qb->expr()->in('nb.news', $this->getEntityManager()
->createQueryBuilder()
->select('n.id')
->from(News::class, 'n')
->where($qb->expr()->neq('n.status', "'active'"))
->getDQL()))
->andWhere('nb.dt < :older')
->setParameter('older', new \DateTime('-1 month'))
->getQuery()
->execute();
Answer the question
In order to leave comments, you need to log in
The doctrine does not know how to make a connection inside a DELETE. this is not supported by all DB vendors. For example, SQLite can't do this: https://www.sqlite.org/lang_delete.html
In my opinion, the best thing you can do is rewrite the query in DQL for readability. Are you sure you need constructs like: $qb->expr()->neq?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question