Codeigniter:更新模型中的多行

时间:2013-06-25 19:48:38

标签: php codeigniter sql-update

我读过类似的问题和答案,但仍然不知道,所以不要拍摄:)

我正在尝试这个:

Array ( [0] => Array ( [id] => 3 [paid_date] => 2013-06-25 20:37:35 [statement_status] => P ) [1] => Array ( [id] => 4 [paid_date] => 2013-06-25 20:37:35 [statement_status] => P ) )

$this->db->update_batch('credit_statement', $data, 'id'); 

并返回:

A Database Error Occurred
You must use the "set" method to update an entry.

并且觉得我尊重Codeigniter书:

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name 2' ,
      'date' => 'My date 2'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name 2' ,
      'date' => 'Another date 2'
   )
);

$this->db->update_batch('mytable', $data, 'title'); 

// Produces: 
// UPDATE `mytable` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE 
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date` END
// WHERE `title` IN ('My title','Another title')

我做错了什么?

1 个答案:

答案 0 :(得分:3)

在update_batch方法中调用$ data变量时,没有设置$ data变量。

改变这个:

Array ( [0] => Array ( [id] => 3 [paid_date] => 2013-06-25 20:37:35 [statement_status] => P ) [1] => Array ( [id] => 4 [paid_date] => 2013-06-25 20:37:35 [statement_status] => P ) )

对此:

$data = Array ( [0] => Array ( [id] => 3 [paid_date] => 2013-06-25 20:37:35 [statement_status] => P ) [1] => Array ( [id] => 4 [paid_date] => 2013-06-25 20:37:35 [statement_status] => P ) );