【问题标题】:Check whether update_batch() is successful in CodeIgniter在 CodeIgniter 中检查 update_batch() 是否成功
【发布时间】:2012-05-11 01:13:54
【问题描述】:

我正在 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

从批量更新功能中询问似乎是一件相当简单的事情。有什么我遗漏的还是我应该编写自己的批量更新代码?

【问题讨论】:

  • 您是否可以选择使用事务而不是 CI 的 update_batch
  • 我正在与 MyISAM 合作开展这个项目,因为它用于一个不会经常更新的简单 CMS。不过很好,我可能会考虑切换到 InnoDB。

标签: php codeigniter activerecord


【解决方案1】:
    $this->db->trans_start();
    $this->db->update_batch($table, $update, $variable);
    $this->db->trans_complete();        
    return ($this->db->trans_status() === FALSE)? FALSE:TRUE;

希望这会有所帮助!干杯!

【讨论】:

  • 如果可行,是否可以将其标记为解决方案?或者这个答案可能有问题?
  • 只是想知道?你为什么在这个例子中使用事务?文档示例没有显示交易的必要性。
【解决方案2】:

使用一个简单的指令这将返回真或假

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
相关资源
最近更新 更多