Trying to figure out Models and data validation in MVC

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Trying to figure out Models and data validation in MVC

Post by Cirdan »

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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trying to figure out Models and data validation in MVC

Post by Christopher »

It is certainly OK to have your Model methods just return an array -- if your View just takes the array and uses it to generate output. However, if you are returning arrays and find that your View is doing additional processing on the data that does not seem like Presentation logic then you should move that logic into the Model. That is where you might want to encapsulate that array of data in an object to give the Presentation code a clear interface that decouples that code from the details (and therefore changes) in the underlying data. Examples might be: if you find the View inspecting the user data to see if the user account is active or what group the user is in, passing specific fields to other Models, etc.
(#10850)
Cirdan
Forum Contributor
Posts: 144
Joined: Sat Nov 01, 2008 3:20 pm

Re: Trying to figure out Models and data validation in MVC

Post by Cirdan »

Okay I see. So if I need to find the name of the group that the user belongs to, I perform that query inside the user model? Currently I perform that query on the group model in the controller.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trying to figure out Models and data validation in MVC

Post by Christopher »

It can be done in either the user or group class ... up to you. And you could also have the user class use a group object internally, or vice versa. That is sort of a separate design discussion.

The point I was making is to not have your non-presentation logic leaking out of the Model into the View/Controller. There is this idea of Ask Don't Tell that may be a helpful guide for you.
(#10850)
Post Reply