Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP How to get latest auto incremented id after saving data
    text
    copied!<p>I have checked <a href="https://stackoverflow.com/questions/11127210/cakephp-saving-data-to-database">this</a> question as well and <a href="https://stackoverflow.com/questions/979709/what-is-the-equivalent-to-getlastinsertid-in-cakephp">this</a> one as well. I am trying to implement the model described in <a href="https://stackoverflow.com/questions/6541302/thread-messaging-system-database-schema-design">this</a> question. </p> <p>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. </p> <p>This is what I am trying to do in Thread Model:</p> <pre><code>function saveThreadAndGetId(){ //$data= array('Thread' =&gt; array()); $data= array('id' =&gt; ' '); //Debugger::dump(print_r($data)); $this-&gt;save($data); debug('id: '.$this-&gt;id); $threadId = $this-&gt;getInsertID(); debug($threadId); $threadId = $this-&gt;getLastInsertId(); debug($threadId); die(); return $threadId; } $data= array('id' =&gt; ' '); </code></pre> <p>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?</p> <p>Initially I was doing the query thing in the message controller:</p> <pre><code>$this-&gt;Thread-&gt;query('INSERT INTO threads VALUES();'); </code></pre> <p>but then i found out that lastId function doesnt work on manual queries so i reverted.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload