CakePHP 2 assumption concerning AfterSave

90 Views Asked by At

As I have not found a reply to this question even though I have Googled for an answer, I have to ask it here.

Can I assume that I always have ID (PK column) in $this->data[$this->alias]['id'] inside afterSave method?

If I cannot assume this, in what situation afterSave would not give PK?

Thank you for your help.

1

There are 1 best solutions below

2
On BEST ANSWER

In afterSave() you want to be using $this->id which is always set as afterSave is only called if the save is successful, i.e. a row was either inserted or updated in the database.

If you look at the code for save() in Model.php you'll see that if the save isn't successful the method returns before the afterSave callback is initiated.