views:

190

answers:

2

Need to perform DQL delete from multple 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:
http://stackoverflow.com/questions/2247905/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 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.

A: 

If they're related so that $ids will remove both:

 ->delete()
 ->from('ComRealtyObjects c, c.ComRealtyObjectsPhones p')
 ->whereIn('c.id', '?', $ids)

That should do the job.

Tom
A: 

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

jamil