Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter尝试捕获事务_Php_Mysql_Codeigniter_Transactions - Fatal编程技术网

Php Codeigniter尝试捕获事务

Php Codeigniter尝试捕获事务,php,mysql,codeigniter,transactions,Php,Mysql,Codeigniter,Transactions,我有这个事务并尝试catch语句,它检查每个$\u POST['count']的值不小于0*只有当if语句为false且不包含不正确的过去更新时,才会发生回滚 $this->db->trans_begin(); foreach($_POST['ID'] as $val => $r) { //Gets Count $this->db->select('count'); $this->db-

我有这个事务并尝试catch语句,它检查每个
$\u 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();

之后会有insert和update语句,但如果在退出时发现异常,我希望停止整个过程语句不这样做

有几种方法可以做到这一点。也许最简单的就是这个

型号:

//I'm making the function up cause you don't show it
public function do_something(){ 

//all code the same up to here...

if($nc > 0){
    //Updates Count
    $process = array('count' => $nc);

    $this->db->where('table_id', $r);
    $this->db->update('table1', $process);
 } else {
     $this->db->trans_rollback();
     throw new Exception('Model whats_its_name has nothing to process');
 }
模型中的catch语句将捕获数据库类可能抛出的任何异常。(他们会抛出异常吗?我不知道。)

控制器

try{
    $this->some_model->do_something();
}
catch (Exception $e) {
 //catch the exception thrown in do_something()
 //additional handling here
}

尽管如此,在调用
$this->some_model->do_something()之前,最好先检查
$\u POST['count']
以获得适当的值

您使用的是INNODB数据库还是MYISAM数据库?您最感兴趣的是测试$nc>0,如果不是,则停止进程?上面显示的代码是在模型还是控制器中?@DFriend是的,如果捕获到异常并且上面显示的代码是模型,我也需要停止进程。该代码中的任何内容实际上都不会引发错误异常(CodeIgniter也不这样做)。