控制中的Codeigniter事务

时间:2015-04-28 07:10:14

标签: php codeigniter

由于我将以下控件中的多个函数作为单个事务执行,因此我无法将每个函数作为模型中的事务包围。所以我按照以下方式做到了。如果有任何问题,请有人告诉我。现在工作正常,但不知道是否会出现任何并发问题或者还有其他方法吗?

if(isset($_POST['btnsave']))
        {
            $mcodes = $_POST['tblmcode'];
            $count = count($mcodes);
            //echo $count;

            $issue = new Materialissue_model();

            $this->db->trans_start(); //Here starts my transaction
            $issue->setIssuecode($this->input->post('txtissuecode'));

            if($issue->checkNoExistence()) {

                $issue->setDate($this->input->post('txtdate'));
                $issue->setCustomer($this->input->post('txtcustomer'));
                $issue->setFromlocation($this->input->post('txtlocation'));
                $issue->setResponsible($this->input->post('txtresponsible'));
                $issue->setComments($this->input->post('txtcomments'));
                $issue->setTotal($this->input->post('txttotal'));
                $issue->setUser($this->session->userdata('username'));
                $issue->setStatus($this->input->post('txtstatus'));

                for ($i = 0; $i < $count; $i++) {

                    $issue->setMaterialcode($_POST['tblmcode'][$i]);
                    $issue->setMaterialname($_POST['tblmname'][$i]);
                    $issue->setCost($_POST['tblcost'][$i]);
                    $issue->setQty($_POST['tblqty'][$i]);
                    $issue->setSubtotal($_POST['tblsubtotal'][$i]);
                    $issue->saveIssueDetail();

                    $stock = new Materialstock_model();
                    $stock->setItemcode($_POST['tblmcode'][$i]);
                    $stock->setItemlocation($this->input->post('txtlocation'));
                    $stock->setQty($_POST['tblqty'][$i]);
                    $stock->setRefno($this->input->post('txtissuecode'));
                    $stock->setLasttransaction('MATERIAL-ISSUE');
                    $stock->updateMaterialIssueStock();


                    $transaction = new Transaction_model();
                    $transaction->setDescription("MATERIAL-ISSUE");
                    $transaction->setItemcode($_POST['tblmcode'][$i]);
                    $transaction->setRecqty("0");
                    $transaction->setTransqty("0");
                    $transaction->setIssueqty($_POST['tblqty'][$i]);
                    $transaction->setDate($this->input->post('txtdate'));
                    $transaction->setUser($this->session->userdata('username'));
                    $transaction->saveMaterialTransaction();


                }

                $result = $issue->saveIssue();
                $this->db->trans_complete(); //Here ends my transaction
                if ($result) {

                    $message = new Message_model();
                    $data['message'] = $message->recordadded;
                    $data['type'] = "success";
                    $data['returnpage'] = base_url() . "index.php/materialissue_control/show";
                    $data["print"] =  base_url() . "index.php/Notegenerator_control/showMaterialIssueNote?code=".$issue->getIssuecode();
                    $this->load->view('messageprint_view', $data);

                }

            }else{

                $message = new Message_model();
                $data['message'] = $message->issuecodeexists;
                $data['type'] = "error";
                $data['returnpage'] = base_url() . "index.php/materialissue_control/show";
                $this->load->view('message_view', $data);


            }

        }

1 个答案:

答案 0 :(得分:0)

我更喜欢使用触发器来处理一个控制器中的许多功能,这使得mycode干净且易于跟踪。例: 用户写文章时,此动作将调用模型write_article中的一个动作与1个事务组合,但此函数运行任何查询: 插入帖子 2.lock count post category 3.lock count用户帖子 4.按日期计数 代码中的示例

public function write_article($post) {
    $this->cms->db->trans_start(TRUE);
        $this->cms->db->set('content', $posts->get_content());
        $this->cms->db->insert('t_posts');
    $this->cms->db->trans_complete();
    if($this->cms->db->trans_status() === TRUE){
        $this->cms->db->trans_commit();
    }else{
        $this->cms->db->trans_rollback();
    }
}

关于触发器的这个参考 www.sitepoint.com/how-to-create-mysql-triggers