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: PHP Code: $this->db->trans_start(TRUE); // Query will be rolled back 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. PHP Code: // Instead of using the $this->db->query('AN SQL QUERY...'); I am using a function: PHP Code: // Adding the product and returning the aggregate product ID PHP Code: // Adding Product Details 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. Thanks for your help
Looks like it should work fine. CodeIgniter doesn't care where you start and finish your transaction.
(06-21-2017, 07:59 AM)skunkbad Wrote: Looks like it should work fine. CodeIgniter doesn't care where you start and finish your transaction. What does it mean, from the moment the transaction starts until it ends, regardless if I use functions, CI is able to detect that? I've seen this example in a POST comment, it looks like it may also be an option, but I want to be sure to do this. Example and the code: http://www.itgo.me/a/9149152029127615409...ansactions PHP Code: $this->db->trans_begin();
Yes. Think about code execution. Just because code execution goes in and out of functions doesn't mean that the DB class isn't going to handle what it's told to do. Functions/methods do limit variable scope, help organize your code, and have other benefits. Functions/methods aren't going to interfere with your transactions.
(06-21-2017, 09:04 AM)skunkbad Wrote: Yes. Think about code execution. Just because code execution goes in and out of functions doesn't mean that the DB class isn't going to handle what it's told to do. Functions/methods do limit variable scope, help organize your code, and have other benefits. Functions/methods aren't going to interfere with your transactions. Perfect, thank you very much. (06-21-2017, 09:04 AM)skunkbad Wrote: Yes. Think about code execution. Just because code execution goes in and out of functions doesn't mean that the DB class isn't going to handle what it's told to do. Functions/methods do limit variable scope, help organize your code, and have other benefits. Functions/methods aren't going to interfere with your transactions. I have performed the tests but if an insert fails all queries are executed and does not undo the queries that have to be undone in order to maintain the integrity of the data. PHP Code: <?php
Could calling this be causing the problem?
PHP Code: $this->db->affected_rows() Maybe the call to affected rows is causing the query to be commited?
Also make sure your tables are InnoDB. If they are MyISAM then transactions don't work.
You can use TRANSACTION or INSERT_LAST_ID. When you want to insert data in multiple tables those two are useful. I can't provide a sample snippet for better understanding but I am sure you can find huge samples when you do a research. The difference between the 2 that I don't know.
|
Welcome Guest, Not a member yet? Register Sign In |