Molecular Man
Molecular Man

Reputation: 22386

How to delete all joined entities in the single DQL

I have such a dql statement:

DELETE i, p, d, d2, s
  FROM OverseerMainBundle:Image i 
    JOIN i.plates p 
    JOIN i.doc d 
    JOIN p.doc d2
    LEFT JOIN p.symbols s 
  WHERE d.tag LIKE :tag

If I replace DELETE with SELECT the query works as expected. However, when I use DELETE it gives me an error

[Semantical Error] line 0, col 7 near 'i, p, d, d2,': Error: Class 'i' is not defined.

Upvotes: 2

Views: 222

Answers (1)

lxg
lxg

Reputation: 13107

JOINs are not supported for DELETE queries in DQL, because some of the supported RDBMSes don't support it.

Unfortunately, you must first select the IDs of the elements to delete, then do a DELETE FROM foo WHERE bar IN (?1) and pass the array with the IDs as parameter.

Remember to use an explicit transaction when deleting from multiple tables.

An alternative would be a native query, given that the underlying RDBMS supports JOINs in DELETE queries.

Upvotes: 1

Related Questions