tags:

views:

767

answers:

3

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]);

A: 

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.

dhofstet
A: 

If you use the the save() method you can get the id like so:

$this->Model->save($data);
$id = $this->Model->id;
Shard
A: 

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

centr0