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?

Thanks,



81.1k
Accepted
answer

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



18.1k

Thanks for the prompt feedback! ;)