5

First of all, I just want to mention that this is not an "issue" at all. Deleting with Doctrine DBAL is not a problem and I am able to do it.

What I actually wanted to know is if there is way to delete multiple rows at once without have to loop for example an array.

For my project I use Silex and the Doctrine DBAL

Here goes an example how I currently do it:

$toDelete = array(1,2,3,4);

foreach($toDelete as $id){
   $this->app['db']->delete('table_name',array('id' => $id ));
}

Is there any approach to avoid the looping?

bzin
  • 1,921
  • 2
  • 14
  • 15
  • probably this helps: http://doctrine-orm.readthedocs.org/en/latest/reference/batch-processing.html#bulk-deletes – iVenGO Jul 10 '15 at 15:56
  • 1
    DBAL supports WHERE id IN(?) with an array of ids: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#list-of-parameters-conversion – Cerad Jul 10 '15 at 18:05

2 Answers2

9

You can use list of parameters: http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html#list-of-parameters-conversion

$connection->executeQuery('DELETE FROM table_name WHERE id IN (?)',
    array(array(1,2,3,4)),
    array(\Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
);

or with query builder:

$builder = $connection->createQueryBuilder()
    ->delete('table_name')
    ->where('id in (:ids)')
    ->setParameter(':ids', array(array(1,2,3,4)), Connection::PARAM_INT_ARRAY);
$builder->execute();
Iurii Ant
  • 877
  • 8
  • 15
1

Not without writing your own query.

$app["db"] (assuming default silex/doctrine setup) is a doctrine connection, so you have the possibility to execute any query you want.

Maerlyn
  • 33,687
  • 18
  • 94
  • 85