Should Models output application errors? |
[eluser]the real rlee[/eluser]
Hi guys, was wondering if someone could explain this to me. Its my understand in MVC that Models should be used for data acquisition only - they should be outputting anything. My issue is what about application errors? Like say i have a get_user_by_id() function in my Model and it's an invalid id. Should i use show_error() in my Model or return FALSE and use show_error() in my Controller ? Thanks in advance!
[eluser]sikkle[/eluser]
I think in the MCV approach and in fact to always control what you do, get TRUE or FALSE from your model, and do whatever you want after in the controller to control it.
[eluser]the real rlee[/eluser]
Yeah that's what i was initially thinking. But then the Controller gets crowded with conditional checks. And if the error is related to the data acquisition almost makes sense to prompt in the model... Code: function get_user_id(){
[eluser]sikkle[/eluser]
Model Code: function getLastConnection($user_id) Controller Code: if (!$this->wrawra->getLastConnection()) {redirect('welcome', 'location') OR show message or WHATEVER.;} Maybe i'm wrong you tell me
[eluser]Michael Wales[/eluser]
I always get TRUE or FALSE from my model (unless I am requesting a record, or series of records, then I return an object to represent that or FALSE). I then use my Controller to show_error() if need be.
[eluser]the real rlee[/eluser]
Hmm yeah i get what your both saying. I was just trying to avoid testing the result everytime I called my model function within my Controller :/. MVC is great but knowing where to put things is difficult at the best of times lol
[eluser]Luipaard[/eluser]
How about exceptions, if you're using PHP 5 that is. You throw the error in the model class, and you can choose to handle it in the controller, or just let PHP raise it. Model Code: function get_user_id(){ Controller Code: function index() { Exceptions
[eluser]the real rlee[/eluser]
Hmmm yeah i like your thinking Luipaard, unfortunately the app im working on is in PHP4 .. Really should move to PHP5 |
Welcome Guest, Not a member yet? Register Sign In |