Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DQL delete from multiple tables (doctrine)

I need to perform DQL delete from multiple related tables. In SQL it is something like this:

  DELETE r1,r2 
  FROM ComRealty_objects r1, com_realty_objects_phones r2 
  WHERE r1.id IN (10,20) AND r2.id_object IN (10,20)

I need to perform this statement using DQL, but I'm stuck on this. :(

<?php
$dql = Doctrine_Query::create()
     ->delete('phones, comrealtyobjects')
     ->from('ComRealtyObjects comrealtyobjects')
     ->from('ComRealtyObjectsPhones phones')
     ->whereIn("comrealtyobjects.id", $ids)
     ->whereIn("phones.id_object", $ids);
echo($dql->getSqlQuery());
?>

But DQL parser gives me this result:

DELETE FROM `com_realty_objects_phones`, `ComRealty_objects` 
WHERE (`id` IN (?) AND `id_object` IN (?))

Searching google and stack overflow I found this(useful) topic:
What is the syntax for a multi-table delete on a MySQL database using Doctrine? But this is not exactly my case - there was delete from single table.

If there is a way to override DQL parser behaviour? Or is there maybe some other way to delete records from multiple tables using doctrine?

Note: If you are using doctrine behaviours(Doctrine_Record_Generator), you need first to initialize those tables using Doctrine_Core::initializeModels() to perform DQL operations on them.

like image 883
singer Avatar asked Nov 06 '22 12:11

singer


1 Answers

try:

$dql = Doctrine_Query::create()
     ->from('ComRealtyObjects comrealtyobjects')
     ->from('ComRealtyObjectsPhones phones')
     ->whereIn("comrealtyobjects.id", $ids)
     ->whereIn("phones.id_object", $ids);
$result= $dql->execute();
$result->delete();

and tell me how it goes

like image 155
jamil Avatar answered Dec 26 '22 09:12

jamil