Welcome Guest, Not a member yet? Register   Sign In
Advantages of Models
#21

[eluser]sl3dg3hamm3r[/eluser]
IMHO form validation is not the job of a model. I do this within a controller. Or, if the wish of a slim controller arises, I move it to a helper or library. In my models, I expect validated parameters, in order to keep the model-function as unspecialized as possible.
#22

[eluser]xwero[/eluser]
Colin i separate the models because i'm not a fan of big files. A second reason is that i wanted to separate the reusable methods from the application dependent methods. The separation between validation and view is because of the use of the controller and method name to activate the actions that have to take place.

sl3dg3hamm3r and Andy isn't input data? And isn't the models dogma : if it's data put it in me?
I'm not starting a discussion. You do what you think is best. I gave my view on what a model is and what it should do.

My track record of writing validation code is controller, library, model. I feel most comfortable putting it in a 'model' because i load them using the controller and method name, which means my controller methods are empty most of the time.
#23

[eluser]Colin Williams[/eluser]
Neither do I, sl3dge, but I think it's okay for a model to say, "Here are the rules," even though it doesn't check them against anything. I've yet to find a practical purpose for this though. For example, I only ever accept and save a blog post through the blog controller. Only the blog controller needs the rules, so why abstract it out to the model?

And to your question, Andy, I think it's okay to let the model fail if it was given bad data. However, the model ought to be very flexible so it can accept less-than-perfect formed data. And while I don't have a model validate data, I do see one of its roles being to prepare data; this goes back to being flexible. Like, running strtotime on a date field before saving data that should be a timestamp; getting rid of fields that don't exist in the table (mapping to a schema); trimming, etc.

I cover a lot of this in a piece that I finished 9/10s of. It was for Michael Wales' CI Advent. I still plan on publishing it, and will probably ask Michael to do it on his blog. Gonna be kinda silly with its Christmas theme, though... Maybe I can make it New Years slanted instead.
#24

[eluser]Andy Chapman[/eluser]
Thanks guys. Colin, let us know if you publish that article - I for one am keen on reading it, as I want to get my head around the best design practices for MVC within CI ASAP.
#25

[eluser]nmweb[/eluser]
I still feel models should do the bulk of an application and controllers rarely do anything more than passing stuff through. If I ask a controller for a list of blog posts it should ask the model for those and send them to the view. Depending on the parameters used it should use the rss, xml, json or html view.

Having the rules in the blog controller because that's the only place you'll need them might be convenient. For maintenance, extendability and consistency it might be not so. It's a matter of preference like so many things.




Theme © iAndrew 2016 - Forum software by © MyBB