Search code examples
doctrinesymfony1inner-joinsql-delete

Symfony Doctrine_Query DELETE with INNER JOIN


I am using symfony+doctrine, and I want to perform a delete query with a join. See below for my code I am currently using that works fine.

$sql = 'DELETE a
 FROM a
 INNER JOIN b ON a.b_id = b.id
 WHERE b.c_id = :c_id';

$pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$stmt = $pdo->prepare($sql);
$params = array('c_id' => $c_id);
$stmt->execute($params);

Anyone know how I can do this using:

Doctrine_Core::getTable('a')

Or

Doctrine_Query::create()->delete()->from('a')

I have had no luck with either.

I just don't really want to use raw SQL in my app.


Solution

  • Something like this should do it

    Doctrine_Query::create()
        ->delete('a a')
        ->innerJoin('a.b b')
        ->where('b.c_id = ?', $c_id)
        ->execute()