Welcome Guest, Not a member yet? Register   Sign In
Transactions not rollback doesn't work with multiple model in codeigniter?
#1

[eluser]ted wong[/eluser]
I have a model called post, and I will pass an array to post model, and it will creates tags, and create in the tag model. Which means...The situation like that:

create a post---> post model (create a tag for me)---> tag model

So, when there is something wrong, I return back a FALSE, so that I can rollback the db....But the problem is, when I rollback, only the post model is rollback, but not the tag model.... Here is the code:

The Controller:

Code:
$this->db->trans_start();  

$this->Post_model->create($post);

//return FALSE = not success        
$new_post_id = $this-> Post_model->create($post);

$this->db->trans_complete();

//Login      
if (($this->db->trans_status() === FALSE) || ($new_post_id === FALSE))
{    
    $this->db->trans_rollback();
    echo('fail');
}


The post model:


Code:
$CI =& get_instance();
    $CI->load->model('Tag_model');  

    $array_of_tag_id     = $CI->Tag_model->create_by_string($tag_string);

    if($array_of_tag_id === FALSE)
    {
        return FALSE;
    }




Theme © iAndrew 2016 - Forum software by © MyBB