Search code examples
phpmysqlidiorm

raw_query - update multiple colums - doesn't work with Idiorm


I'm using Idiorm - a very simplistic ORM. I'm trying to update multiple rows in a single query. Idiorm doesn't support this, so I'm left with either n queries or a raw_query statement.

I choose the latter.

However, I can't seem to make it work. They query in itself isn't very complex:

UPDATE products 
SET discount_green = some_value 
WHERE category_id = some_other_value 
AND discount_green != yet_another_value
AND has_double_discount != 1

In Idiorm syntax it would look like this:

ORM::for_table('products')
        ->raw_query(
        "UPDATE products 
         SET discount_green = :some_value 
         WHERE category_id = :some_other_value  
         AND discount_green != :yet_another_value 
         AND has_double_discount != 1",

        array(
            'some_value' => $some_value,
            'some_other_value' => $some_other_value,
            'yet_another_value' => $yet_another_value,
        )
    );

The for_table param could very well be NULL.

I have tried:

  1. Simply executing the query without binding the params as in a whole complete query with static parameters - did not work
  2. Using no ORM - works fine.
  3. Using question marks instead of the : notation - doesn't work.

With that being said, I'm probably missing something obvious here. Any nudging in the right direction is greatly appreciated.

I have looked into options like raw_execute, didn't have much luck with that either.

It doesn't particularly matter, but all the values are numeric.


Solution

  • If you prefer a single query, raw_execute is the way to go. You could do something like the following.

    ORM::raw_execute("UPDATE products " .
                     "SET discount_green = :some_value  " .
                     "WHERE category_id = :some_other_value " .
                     "AND discount_green != :yet_another_value  " .
                     "AND has_double_discount != 1",
        array (
          "some_value" => $some_value,
          "some_other_value" => $some_other_value,
          "yet_another_value" => $yet_another_value
        )
    );