尝试使用$ model-> save()时脚本死亡

时间:2017-01-03 15:37:03

标签: php phalcon

我正在尝试使用$ model-> save()..但是我无法获取文档之类的错误消息,而是出现错误消息,其中包含跟踪错误,然后脚本就会死亡。

例如我正在尝试使用此代码:

$user = new Users();
$user->name = "Name";

if(!$user->save()){
   //do stuff if there is an error
}

问题是我总是得到这样的验证错误并且脚本终止并且永远不会返回false以输入if条件:

password is required
#0 ..../UsersController.php(106): Phalcon\Mvc\Model->save()
#1 [internal function]: ....\UsersController->createAction()
#2 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(....\UsersController), 'createAction', Array)
#3 [internal function]: Phalcon\Dispatcher->_dispatch()
#4 [internal function]: Phalcon\Dispatcher->dispatch()
#5 ....\public\index.php(41): Phalcon\Mvc\Application->handle()
#6 {main}**

2 个答案:

答案 0 :(得分:0)

我终于找到了解决方案,我发现在services.php的某个地方 此行已启用,导致异常的原因被抛出 Phalcon\Mvc\Model::setup(['exceptionOnFailedSave' => true]并将其变为false后,我可以轻松添加条件并使用此代码检查错误消息。

if ($model->save() === false) {
    $messages = $model->getMessages();
    foreach ($messages as $message) {
        echo $message, "\n";
    }
}

答案 1 :(得分:0)

您可以使用

发送Flash错误
$this->flash->error("too bad! the form had errors");
$this->flash->success("yes!, everything went very smoothly");
$this->flash->notice("this a very important information");
$this->flash->warning("best check yo self, you're not looking too good.");

参见文档 https://docs.phalconphp.com/en/latest/reference/flash.html