![]() |
insertID bad return - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: insertID bad return (/showthread.php?tid=86648) |
insertID bad return - keto - 02-06-2023 [Google translation] Hi, I'm migrating from CI3 to CI4. I do an insert with the builder and then try to retrieve the primary key ID number. But the returned ID is not good, I should have a value in the 5XX but I have a value in the 2479XX My function in model : Code: public function insertFiche($val1, $val2){ Thanks for your help RE: insertID bad return - kenjis - 02-07-2023 Why do you know 2479XX is bad? It is just a number that your database returned. RE: insertID bad return - keto - 02-07-2023 Hi Kenjis, I'll try to explain my problem a bit more: On CI3, when I do an insert and then use insert_id(), it returns me the id of the primary key in auto increment . In my example, looking at the DB, it should have returned 541. But when I try to migrate to CI4, and use the insertID() function after an insert, the returned value doesn't match at all. It gave me a much higher value. I hope to be clearer with this explanation and that the Google translation does not break my sentences. RE: insertID bad return - kenjis - 02-07-2023 Your code seems good, and should be return correct insert id. But in fact, it is not. I don't know why, but I recommend your check all queries in database log (or CI4's debug toolbar). If the insert id is wrong, there might be another insert before you call insertID(). RE: insertID bad return - ozornick - 02-07-2023 Alternatively, try to find ID 2479 X X in all tables, if there are not so many of them. This will help you find the request that returned the wrong ID RE: insertID bad return - keto - 02-07-2023 (02-07-2023, 04:19 AM)kenjis Wrote: If the insert id is wrong, there might be another insert before you call insertID().That's it ! THANKS ! I had completely forgotten that I log each modification in BBD by an insert in a table 'log_bdd' via an event 'Events::on('DBQuery', function($query) {‘. The IDs correspond. Now I have to figure out how to make the log not overwrite the ID in the object or keep the ID to rewrite it after the log. RE: insertID bad return - davidb2002 - 07-09-2024 (02-07-2023, 05:16 AM)keto Wrote:(02-07-2023, 04:19 AM)kenjis Wrote: If the insert id is wrong, there might be another insert before you call insertID().That's it ! THANKS ! I had completely forgotten that I log each modification in BBD by an insert in a table 'log_bdd' via an event 'Events::on('DBQuery', function($query) {‘. I was wondering if you managed to find a solution to this as I'm experience the same issue due to a DBQuery event I have created which inserts a record. |