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?
Not开发者_运维百科e: 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.
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
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.
精彩评论