CodeIgniter Framework update transaction (transaction) bug and workaround _php instance

Source: Internet
Author: User

Because the CI transaction judge error rollback condition is whether the statement executes successfully, while the update operation, even if the effect of the number of 0,sql statement execution result is still 1, because it succeeded, but only the effect of the number of bars is 0.

Here's how to solve this problem:

For a transaction that executes many statements at once

Just under the update operation, depending on whether the number of affected bars is 0来 or not, let's assume that the second statement is an update operation.

Copy Code code as follows:

Manual mode with Codeigniter transactions
$this->db->trans_strict (FALSE);
$this->db->trans_begin ();

$this->db->query (' SELECT ... ');//select operation does not require special handling
$this->db->query (' INSERT ... ')//insert error will have Codeigniter automatic processing

$this->db->query (' UPDATE ... ');
if (! $this->db->affacted_rows ()) {//The UPDATE failed on the above rollback
$this->db->trans_rollback ();
//@todo Exception Handling section
Exit ();//need to terminate or jump out, lest the following SQL code continue to execute!
}

$this->db->query (' DELETE ... ');
if (! $this->db->affacted_rows ()) {//The above DELETE fails rollback
$this->db->trans_rollback ();
@todo Exception Handling Section
Exit ();//need to terminate or jump out, lest the following SQL code continue to execute!
}

$this->db->query (' SELECT ... ');//select operation does not require special handling
$this->db->query (' INSERT ... ')//insert error will have Codeigniter automatic processing

if ($this->db->trans_status () = = TRUE) {
$this->db->trans_commit ();
} else {
$this->db->trans_rollback ();
//@todo Exception Handling section
}

If the statements executed at one time are not many, you can make a final decision to decide on the rollback

If there is no update operation in the statement, you can use an automatic transaction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.