CakePHP - How to use onError in a model
I have created my own datasource that pulls data from the web api and I am now looking at implementing error handling.
In the data source, I call $ model-> onError (). In the model, I have created an onError method and I can get the error information using $ this-> getDataSource () -> error;
However, I cannot redirect or set the flash message because this can only happen in the controller, so what should I do to report the error to the user?
a source to share
I would compile all the errors in the model into a bunch of errors, possibly an array. Then I would set it as a variable in the model.
This way, in my controller, I can do $this->Model->getErrors()
or similar to read and return the value set in the model.
Then, in my controller, beforeFilter()
I would check if there are any errors, and if there are, then push them to flash.$this->Session->setFlash($this->Model->getErrors(),'default',array('class'=>'error-message'));
a source to share