how i can get the inserted id when I insert a record in this way using cakephp $this->query (" insert into [tablename] ([colname]) values([colvalue]);
Assuming you are using MySQL you could perform the following query after you did the insert:
$array_with_id = $this->query('select last_insert_id() as id;');
But as mentioned by kouak, the usual way to insert data is to use the save() method. If you use this method, the id of the inserted record will then be automatically available in the $id property of the respective model.
If you use the the save() method you can get the id like so:
$this->Model->save($data);
$id = $this->Model->id;
Cake's model class has a function that gets the last inserted id:
$this->Model->getLastInsertID()
line 2584 in {project-folder}/cake/libs/model/model.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With