简体   繁体   中英

CodeIgniter transactions with multiple functions

I need to add a product in the "products" table, and I get the added product ID and then add the details of that product in the table "product details"

In the official page of CodeIgniter they show how to do this but without using functions, eg:

$this->db->trans_start(TRUE); // Query will be rolled back
$this->db->query('AN SQL QUERY...');
$this->db->trans_complete();

I need to use functions and still maintain integrity that if one of the two queries fails then the records are not added, I have prepared the following code and I want to know if in this way CodeIgniter will be able to detect if the query fails or not to undo the records Or if there is another best practice or other way of doing it.

Instead of using the $this->db->query('AN SQL QUERY...') I am using a function:

public function init_product()
{
    $this->db->trans_begin();
    $this->new_product();
    $this->new_product_details();

    if ($this->db->trans_status() === FALSE)
        $this->db->trans_rollback();
    } else {
        $this->db->trans_commit();
    }
}

Adding the product and returning the aggregate product ID:

public function new_product()
{      
    $data = array(            
        'name' => $this->input->post('name'),
        'details' => $this->input->post('details'),
        'price' => $this->input->post('price')
    );
    $this->db->insert('products', $data);
    if($this->db->affected_rows()) {            
        return $this->db->insert_id();
    }
    return false;
}

Adding Product Details:

public function new_product_details()
{      
    $data = array(            
        'product_id' => $this->new_product(),
        'user_id' => $this->session->id_user
    );
    $this->db->insert('products', $data);
    if($this->db->affected_rows()) {            
        return true;
    }
    return false;
}

As you specify, I need to know if this way is functional even though I do not follow the example as done in CodeIgniter, and if using these functions CodeIgniter can detect whether or not queries or insertions in the database fail, also if they can give me some better example.

Why do you want to do this? Won't work. I saw you use the same table so why don't you do this in the initial function and return the insert ID if you need it, something like this:

public function init_product()
{
    $this->db->trans_begin();

    $data = array(            
        'name' => $this->input->post('name'),
        'details' => $this->input->post('details'),
        'price' => $this->input->post('price'),
        'user_id' => $this->session->id_user
    );

    $this->db->insert('products', $data);

    if ($this->db->trans_status() === FALSE)
        $this->db->trans_rollback();
    } else {
        $this->db->trans_commit();
    }

    return $this->db->insert_id();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM