We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon\Mvc\Model::save() generating an exception

In the documentation, the way you check if a model data was saved is by checking the boolean returned by the save() method like so:

if (!$album->save()) {

One would think that save() would catch any exceptions and return it as messages but sometimes an exception will not be caught, like this one for instance:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

So my question is: when should I expect save() to return a boolean and messages and when should I expect it to throw an exception?



You must set up virtual foreign keys to avoid getting exceptions from the database when a constraint fails: http://docs.phalcon.io/en/latest/reference/models.html#virtual-foreign-keys


Thanks for the prompt feedback! ;)