Trying to figure out Models and data validation in MVC
Posted: Wed May 25, 2011 6:13 pm
Currently in my web application, I validate POST data in the controller before passing it to the model. The only time I interact with a model object is when I have to interact with the database fetching, inserting, updating, etc. However, as I'm moving forward with my application, putting validation in the controller is starting to look like a bad idea, especially if I need to manipulate the same data sets between multiple controllers. I decided to do a search on the topic and found this and this this, which made me realize that maybe I'm doing models incorrectly. The way I'm doing it now is each section of the site has it's own model object. There is a "Users" model, "Forum" model, "ACL" model. Whenever I have to do anything with a user or users I use the Model_User. Same for if I need to do anything relating to the forum. These models have methods such as fetchUserByUsername(...) and insertNewUser(...) where I simply handle data through arrays. Kind of like how Codeigniter does it.
Taking the above examples, I should change the models to have a model object that represents a single User, then have another object represent a list of users. Or an object to represent a single blog post and a different object to represent a list of blog posts. This is how I would move validation from the controller and into the model. This is where I start to loose understanding. Does a Roster model create a list of User model objects? I am also unsure about where fetching data occurs. Do I have a model object that will return an User model, or is the User model responsible for fetching it's own data and how do I specify the condition of which user I want to fetch?
I could keep it the way I currently have it setup, and whenever I call a method that inserts/updates data in the database it runs the new data through a validation method in the model. This could get super messy tho with my ugly forum model where I'm managing boards, categories, threads and posts. Any pointers on how I should design my models would be most appreciated.
Taking the above examples, I should change the models to have a model object that represents a single User, then have another object represent a list of users. Or an object to represent a single blog post and a different object to represent a list of blog posts. This is how I would move validation from the controller and into the model. This is where I start to loose understanding. Does a Roster model create a list of User model objects? I am also unsure about where fetching data occurs. Do I have a model object that will return an User model, or is the User model responsible for fetching it's own data and how do I specify the condition of which user I want to fetch?
I could keep it the way I currently have it setup, and whenever I call a method that inserts/updates data in the database it runs the new data through a validation method in the model. This could get super messy tho with my ugly forum model where I'm managing boards, categories, threads and posts. Any pointers on how I should design my models would be most appreciated.