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

Throw an exception on save failed

Hi!

It would be nice to have Model\Manager option to throw an exception on model not saved instead of returning false. To me it's expected model behavior - when unexpected errors happends while working with model, it should be an exception.

Of course it could be done now with events manager like this:

        $di['modelsManager'] = function () {
            $eventsManager = new \Phalcon\Events\Manager();
            $eventsManager->attach('model', function ($event, $model) {
                if ($event->getType() == 'notSaved') {
                    throw new ModelException(implode(", ", $model->getMessages()));
                }   
            }); 
            $modelsManager = new \Phalcon\Mvc\Model\Manager();
            $modelsManager->setEventsManager($eventsManager);
            return $modelsManager;
        };

but I'd prefer to use this way:

        $di['modelsManager'] = function () {
            $modelsManager = new \Phalcon\Mvc\Model\Manager();
            $modelsManager->setSaveExceptions(true);
            return $modelsManager;
        };  

and catch Model\NotSavedException using its getModel() method to get errors:

        } catch (\Phalcon\Mvc\Model\NotSavedException $e) {
            $exception->getModel()->getMessages();

What do you think?

You can simply extend the Model (to create a BaseModel, for example) and add your own save() method.

Sure I do, or just use modelsManager events as shown above. But I don't want to repeat this solution from project to project, so this is feature request as it looks to me quite useful. Save() method may throw exceptions anyway (PDOExceptions, for exampe) and it's a good practice to expect and catch them too, so why don't handle any variant of Model errors in one way?



98.9k
Accepted
answer

Try:

Phalcon\Mvc\Model::setup(['exceptionOnFailedSave' => true]);

Thanks, thats exactly what I've been talking about! I should dig sources for more undocumented features ;-)

When using Phalcon\Mvc\Model::setup(['exceptionOnFailedSave' => true]);

The exception will be of type Phalcon\Mvc\Model\ValidationFailed. So if you can do something like this:

try {
    // some code that saves models
} catch(\Phalcon\Mvc\Model\ValidationFailed $e) {
    echo $e->getModel()->getSource() . ' table ' . $e->getMessage();
}