Unable to save data in database manually and get latest auto increment id, cakePHP

Posted by shabby on Stack Overflow See other posts from Stack Overflow or by shabby
Published on 2012-11-01T19:34:38Z Indexed on 2012/11/02 23:01 UTC
Read the original article Hit count: 221

Filed under:

I have checked this question as well and this one as well. I am trying to implement the model described in this question.

What I want to do is, on the add function of message controller, create a record in thread table(this table only has 1 field which is primary key and auto increment), then take its id and insert it in the message table along with the user id which i already have, and then save it in message_read_state and thread_participant table.

This is what I am trying to do in Thread Model:

function saveThreadAndGetId(){
    //$data= array('Thread' => array());
    $data= array('id' => ' ');
   //Debugger::dump(print_r($data));
   $this->save($data);
   debug('id: '.$this->id);
    $threadId = $this->getInsertID();
    debug($threadId);
    $threadId = $this->getLastInsertId();
    debug($threadId);
    die();
    return $threadId;
}


$data= array('id' => ' ');

This line from the above function adds a row in the thread table, but i am unable to retrieve the id. Is there any way I can get the id, or am I saving it wrongly?

Initially I was doing the query thing in the message controller:

$this->Thread->query('INSERT INTO threads VALUES();');

but then i found out that lastId function doesnt work on manual queries so i reverted.

© Stack Overflow or respective owner

Related posts about cakephp