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.

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?



84.1k
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 ;-)