在update_batch codeigniter上传递数组作为参数

时间:2015-12-04 02:05:35

标签: arrays codeigniter

我想将数组作为参数传递给模型。我有点困惑,如何传递它们。

CONTROLLER

[{"ID":"68","MATERIAL":"Test","AC":"a"},     {"ID":"69","MATERIAL":"b","AC":"c"}]

要调试它,这是那些数组的结果

public function update_eir_to_cost($id, $material, $ac) {
    $data = array(
        "MATERIAL" => $material,
        "AC" => $ac
    );

    $this->db->trans_start();
    $this->db->where($id);
    $this->db->update_batch('tb_repair_detail', $data);
    $this->db->trans_complete();

    if ($this->db->trans_status() === FALSE) {
        // generate an error... or use the log_message() function to log your error
        echo "Error Updating";
    }
}

这是 MODEL

SELECT *
FROM  (SELECT user_id FROM users) u  -- subquery to clip other columns
LEFT   JOIN (
   SELECT user_id, count(*) AS connections, max(created_at) AS latest_created_at
        , bool_or(pl = 'a') AS connected_to_a
        , bool_or(pl = 'b') AS connected_to_b
        , bool_or(pl = 'c') AS connected_to_c
   FROM (       SELECT user_id, created_at, 'a'::"char" AS pl FROM platform_a
      UNION ALL SELECT user_id, created_at, 'b'               FROM platform_b
      UNION ALL SELECT user_id, created_at, 'c'               FROM platform_b
      ) p1
   ) p2 USING (user_id)
ORDER  BY user_id;

我的情况是,我想使用update_batch更新我的表。

  

实际上,更新材料和AC,其中ID = ID

任何帮助它如此欣赏。

1 个答案:

答案 0 :(得分:0)

您为print_r($execute)显示的结果不正确。这应该是json_encode($execute)的结果。以下是print_r($execute)

的结果
Array
(
    [0] => Array
        (
            [ID] => 68
            [MATERIAL] => Test
            [AC] => b
        )
    [1] => Array
        (
            [ID] => 69
            [MATERIAL] => a
            [AC] => c
        )
)

现在,在您的模型中

public function update_eir_to_cost($data) {
  $this->db->update_batch('tb_repair_detail', $data, 'ID');
  // Produces: 
  // UPDATE `tb_repair_detail` SET `MATERIAL` = CASE
  // WHEN `ID` = '68' THEN 'Test'
  // WHEN `ID` = '69' THEN 'b'
  // ELSE `name` END,
  // `AC` = CASE 
  // WHEN `ID` = '68' THEN 'a'
  // WHEN `ID` = '69' THEN 'c'
  // ELSE `AC` END
  // WHERE `ID` IN ('68','69')
}

查看Updating Data