Phalcon last insert id in aftercreate function

code K.O.

How can i get last insert id, look like this :

$user = new User();
$user->name = 'asdf';
$user->save();
echo $user->id;

But inside models in function aftercreate or beforecreate function :

public function beforecreate(){
// last id ?
}

What I'm trying to do is to logs the sql statement into another table.

Nikolay Mihaylov

You could do this in the afterCreate. Please note the exit; for debugging purpose.

public function afterCreate()
{
    echo $this->id; // 8
    exit;
}   

Unfortunately you can not access the id in beforeCreate because the record is not yet created, thus there is no id yet.

If you really need id in beforeCreate you could try to get current max value of the primary key and increment it with 1. But that's a bit hacky and using afterCreate is the preferred option.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Phalcon last insert id in aftercreate function

From Dev

How to get last insert id with Phalcon?

From Dev

Codeigniter insert_id() function retrieve correctly the last insert id?

From Dev

MySQL Function with LAST_INSERT_ID() giving unexpected results

From Dev

Getting ID of last insert

From Dev

SQL, select last insert ID by LAST_INSERT_ID()

From Dev

Get last insert id of Postgresql

From Dev

Mysqli last insert id not work

From Dev

Insert Returning last ID with PDO

From Dev

PHP - getting last statements PK ID to use in second function Insert statement

From Dev

it is still adding the row as i am using the last_insert_id function on dupliacte key for updating

From Dev

how to select last_insert_id?

From Dev

Get last insert id return null

From Dev

How efficient is Last_insert_id?

From Dev

how to get the last insert ID in linq

From Dev

MYSQL last_insert_id() and concurrency

From Dev

Return the id of the last MySQL insert in PHP

From Dev

Implement a method that generates the last insert id

From Dev

Get ID from last insert into table

From Dev

Nodjs sequalize how to get last insert id

From Dev

LAST_INSERT_ID after restarting MySQL

From Dev

MYSQL last_insert_id() and concurrency

From Dev

SELECT LAST_INSERT_ID() not generating correctly

From Dev

how to get the last insert ID in linq

From Dev

SELECT LAST_INSERT_ID() returning zero

From Dev

use of where cluase for last_insert_id

From Dev

Implement a method that generates the last insert id

From Dev

Java Spring JDBC last_insert_id()

From Dev

How to get the ID of the last insert in MVC?