开发者

CakePHP - How to use onError in Model

I've created a custom datasource which fetches data from a web api, and I'm now looking at implementing error handling.

In the datasource, I'm calling $model->onError(). In the model, I've created the onError method, and I can access error 开发者_C百科details with $this->getDataSource()->error;

However I can't redirect or set a flash message because that can only take place in the controller, so what should I be doing here to communicate the error to the user?


Are the errors relevant to fields in your model? If so, use $this->invalidate($field, $message) in Model::onError()


I would compile all the errors in the model into a set of errors, perhaps in an array. Then I'd set it as a variable in the Model.

Thus in my controller I can then do $this->Model->getErrors() or similar to read and return the value set in the model.

Then in my controller's beforeFilter() I'd check to see if there were any errors, and if there were, push them into the flash. $this->Session->setFlash($this->Model->getErrors(),'default',array('class'=>'error-message'));

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜