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\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,



98.9k
Accepted
answer

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



32.2k

Thanks for the prompt feedback! ;)