【问题标题】:raw_query - update multiple colums - doesn't work with Idiormraw_query - 更新多个列 - 不适用于 Idiorm
【发布时间】:2015-06-05 12:17:34
【问题描述】:

我正在使用Idiorm - 一个非常简单的 ORM。我正在尝试在单个查询中更新多行。 Idiorm 不支持这一点,所以我只剩下n 查询或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

在惯用语法中,它看起来像这样:

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

我试过了:

  1. 仅在不绑定参数的情况下执行查询,就像在带有静态参数的整个完整查询中一样 - 不起作用
  2. 不使用 ORM - 工作正常。
  3. 使用问号代替 : 表示法 - 不起作用。

话虽如此,我可能在这里遗漏了一些明显的东西。非常感谢任何朝着正确方向的推动。

我已经研究过raw_execute 之类的选项,也没有太多运气。

这并不特别重要,但所有值都是数字。

【问题讨论】:

标签: php mysql idiorm


【解决方案1】:

如果您更喜欢单个查询,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
    )
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 2014-08-09
    • 2014-03-15
    相关资源
    最近更新 更多