【发布时间】:2016-02-10 06:33:51
【问题描述】:
我有这个事务并尝试使用 catch 语句检查每个 $_POST['count'] 的值不小于 0。*回滚仅在 if 语句为 false 时发生,它不包括不正确的过去更新
$this->db->trans_begin();
foreach($_POST['ID'] as $val => $r)
{
//Gets Count
$this->db->select('count');
$this->db->from('table1');
$this->db->where('table_id', $r);
$oc = $this->db->get()->row('count');
$nc = (($oc) - $_POST['count'][$val]);
//Gets Total
$this->db->select('cost');
$this->db->from('table1');
$this->db->where('table_id', $r);
$ot = $this->db->get()->row('cost');
$total = ($ot + $total);
try{
if($nc > 0){
//Updates Quantity
$process = array(
'current_count' => $nc,
);
$this->db->where('product_id', $rm);
$this->db->update('products', $process);
}
}
catch(Exception $e){
$this->db->trans_rollback();
$this->session->set_flashdata('error','Production Failed. 1 or more Raw Materials required for production is insufficient.');
exit;
}
}
$this->db->trans_commit();
之后有插入和更新语句,但我想要的是在显然捕获到异常时停止整个过程 exit; 语句没有这样做
【问题讨论】:
-
你使用的是INNODB数据库还是MYISAM数据库
-
您最感兴趣的是测试 $nc >0 并在不是时停止进程?
-
上面显示的代码是在模型还是控制器中?
-
@DFriend 是的,如果捕获到异常并且上面显示的代码是模型,我还需要停止进程
-
该代码中实际上没有抛出异常(CodeIgniter 也不这样做)。
标签: php mysql codeigniter transactions