2012-05-01 98 views
4

我在CodeIgniter的表上運行update_batch(),我想檢查它是否成功。在CodeIgniter中檢查update_batch()是否成功

我使用affected_rows()試過了,但僅計算已被修改,所以它並不完全切斷它的表單字段數量:

$this->db->update_batch("sections", $data, "alias"); 

log_message("debug", "items in form: ".count($data)); 
// items in form: 3 

log_message("debug", "rows updated: ".$this->db->affected_rows()); 
// rows updated: 0-3 
// depending on whether anything was actually changed on the form 

return ($this->db->affected_rows() == count($data)); // unreliable 

這似乎是一個相當簡單的事情從批量更新功能詢問。有沒有我錯過了,或者我應該寫我自己的批量更新代碼?

+0

正在使用Transactions而不是CI的'update_batch'選項嗎? –

+0

我在這個項目上使用MyISAM,因爲它是一個簡單的CMS,不會經常更新。雖然好,但我可能會考慮切換到InnoDB。 – Andrey

回答

0

使用一個簡單的指令,這將返回true或false

return $this->db->update_batch("sections", $data, "alias"); 
+0

這不適用於我最初嘗試使用的當前穩定版本(撰寫本文時爲2.1.0)。不過,看看Git倉庫,它已經在3.0的開發分支中實現了:https://github.com/EllisLab/CodeIgniter/blob/develop/system/database/DB_driver.php – Andrey

4
$this->db->trans_start(); 
    $this->db->update_batch($table, $update, $variable); 
    $this->db->trans_complete();   
    return ($this->db->trans_status() === FALSE)? FALSE:TRUE; 

希望這有助於!乾杯!

+0

如果這個工作,它可以被標記作爲解決方案?或者也許這個答案有問題? – Programster

+0

只是想知道?你爲什麼使用這個例子的交易?文檔示例沒有顯示交易的必要性。 –