Search code examples
phpmysqldatabaseimplodenette

Format SQL query by php implode function


I'm using Nette Framework and I have this commands in action:

    $id = is_array($id) ? implode(', ', $id) : $id;
    $this->database->table('manufacturing')->where('id', $id)->update(array('trash' => '1'));

When I select 4 rows in my Grid and call this action to delete items, it will generate this SQL code:

UPDATE `manufacturing` 
SET `trash`='1' 
WHERE (`id` = '31, 32, 33, 34')

But I need code like this to update all rows:

UPDATE `manufacturing`
SET `trash` = '1'
WHERE ((`id` = '31') OR (`id` = '32') OR (`id` = '33') OR (`id` = '34'));

Is possible to do with implode function? Thank you.

edit: when I made this:

$id = is_array($id) ? implode(' "OR" ', $id) : $id;

it will do this:

UPDATE `manufacturing` 
SET `trash`='1' 
WHERE (`id` = '31 \"OR\" 32 \"OR\" 33 \"OR\" 34')

Solution

  • you can directly pass the array in the where function, it will be transformed to a id IN (1,2,3,xxx) automaticaly.

    Refs : http://doc.nette.org/en/2.2/database-selection#toc-filtering