We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon model - get last ID in create function

I am trying to fetch the insert id, before it's inserted

Let's say I have rows "ID, postid"

Then i would like to add postid to be the same value as the ID, but I encrypt the postid, so it's not really the same, but the value is...

I am trying something like this, but dosent work..

public function beforeValidationOnCreate()
  {
      $this->postid = encrypt($this->id);
  }

Hope you have a solution!

edited Jan '16

:) u cant, id is not generated at this point. So, u can use something like that in model:

    public function afterCreate() {
        $this->postid = encrypt($this->id);
        $this->save();
    }

Hope i understand u. Good luck!

PS: what is that encrypt function?

I use the encryption to hide the "postid" when i use the id to fetch in a Ajax function, so people dosent know the id

I use MCRYPT_RIJNDAE

edited Jan '16

I get it. So... u still can use afterCreate in model- its a good way. Other option is to make unique generated key by phalcon (search Phalcon\Text) and put it in DB table (unique column)- but still u must check for existing key. So- its up to :)

...but why is so important to hide id?