Welcome Guest, Not a member yet? Register   Sign In
insertID bad return
#1

[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){
    $builder = $this->db->table('myTable');
    $data = array(
      'col1' => $val1,
      'col2' => $val2
    );
    $builder->insert($data);
    $insert_id = $this->db->insertID();
    return  $insert_id;
  }

Thanks for your help
Reply
#2

(This post was last modified: 02-07-2023, 04:23 AM by kenjis.)

Why do you know 2479XX is bad?
It is just a number that your database returned.
Reply
#3

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.
Reply
#4

(This post was last modified: 02-07-2023, 04:24 AM by kenjis.)

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().
Reply
#5

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
Reply
#6

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB