mds1256 Posted April 27, 2018 Share Posted April 27, 2018 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. Quote Link to comment https://forums.phpfreaks.com/topic/307218-controller-and-model-paradigm/ Share on other sites More sharing options...
Solution requinix Posted April 28, 2018 Solution Share Posted April 28, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/307218-controller-and-model-paradigm/#findComment-1558151 Share on other sites More sharing options...
mds1256 Posted April 28, 2018 Author Share Posted April 28, 2018 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 Quote Link to comment https://forums.phpfreaks.com/topic/307218-controller-and-model-paradigm/#findComment-1558162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.