raw_query - 更新多列 - 不适用于 Idiorm
raw_query - update multiple colums - doesn't work with Idiorm
我正在使用 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
语句。
我选择后者。
但是,我似乎无法让它工作。他们查询本身不是很复杂:
UPDATE products
SET discount_green = some_value
WHERE category_id = some_other_value
AND discount_green != yet_another_value
AND has_double_discount != 1
在 Idiorm 语法中它看起来像这样:
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,
)
);
for_table
参数很可能是 NULL
。
我试过:
- 像在带有静态参数的整个完整查询中那样不绑定参数而简单地执行查询 - 没有用
- 不使用 ORM - 工作正常。
- 使用问号代替
:
符号 - 不起作用。
话虽如此,我可能遗漏了一些明显的东西。非常感谢任何朝着正确方向的推动。
我已经研究过 raw_execute
之类的选项,但也不太顺利。
没什么特别的,但是所有的值都是数字。
如果您更喜欢单个查询,raw_execute
是最佳选择。您可以执行以下操作。
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
)
);
我正在使用 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
语句。
我选择后者。
但是,我似乎无法让它工作。他们查询本身不是很复杂:
UPDATE products
SET discount_green = some_value
WHERE category_id = some_other_value
AND discount_green != yet_another_value
AND has_double_discount != 1
在 Idiorm 语法中它看起来像这样:
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,
)
);
for_table
参数很可能是 NULL
。
我试过:
- 像在带有静态参数的整个完整查询中那样不绑定参数而简单地执行查询 - 没有用
- 不使用 ORM - 工作正常。
- 使用问号代替
:
符号 - 不起作用。
话虽如此,我可能遗漏了一些明显的东西。非常感谢任何朝着正确方向的推动。
我已经研究过 raw_execute
之类的选项,但也不太顺利。
没什么特别的,但是所有的值都是数字。
如果您更喜欢单个查询,raw_execute
是最佳选择。您可以执行以下操作。
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
)
);