Jump to content

Controller and Model Paradigm


Go to solution Solved by requinix,

Recommended Posts

Just trying to get my head around what should be in a model and controller.

 

So I have currently the model class as a data structure for an entity (person) which performs validation on the data held using the set methods, e.g. making sure Age is > 0 etc

 

The controller creates the model (Person) and has the business login in for a process e.g. retrieve/insert person from/to database.

 

Is the way I am using this correct as I have read that the model should handle the save / retrieve to/from the database.

 

I have also read that the model should have no login in there at all and that it should just be a structure for holding an entity.

 

Just wanting to know the correct way of using MVC.

Link to comment
https://forums.phpfreaks.com/topic/307218-controller-and-model-paradigm/
Share on other sites

  • Solution

The model represents the data, so it's often a database model but it can also be a so-called "business" model: represents data as the application needs to use it, and would use simplistic database models to get the data it needs so it isn't connected directly to the database. But it's not too common to have both.

However a database model isn't limited to strictly reading from and saving to the database. Some people do that, personally I don't. Really the model is the central place for dealing with data - yes, working with the database is a key component of that, but it can also involve testing the data (eg, verifying login credentials) or manipulating the data (eg, changing a user's password) or many other actions.

 

So yes, the model should not be able to log a user into the application because that is beyond the scope of managing its data. It should have a method to test login credentials, because that is directly related to its control of the data, but the controller is what would do the actual logging-in with the session and cookies and whatnot.

  • Like 1

The model represents the data, so it's often a database model but it can also be a so-called "business" model: represents data as the application needs to use it, and would use simplistic database models to get the data it needs so it isn't connected directly to the database. But it's not too common to have both.

However a database model isn't limited to strictly reading from and saving to the database. Some people do that, personally I don't. Really the model is the central place for dealing with data - yes, working with the database is a key component of that, but it can also involve testing the data (eg, verifying login credentials) or manipulating the data (eg, changing a user's password) or many other actions.

 

So yes, the model should not be able to log a user into the application because that is beyond the scope of managing its data. It should have a method to test login credentials, because that is directly related to its control of the data, but the controller is what would do the actual logging-in with the session and cookies and whatnot.

Thanks for that, yeah that is what I thought. The controller is the business logic which can involve different models, the model is for structuring the data and performing validation and formatting the data (whether it is stored in a database or not).

 

Thanks again

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.