Welcome Guest, Not a member yet? Register   Sign In
Model and Form
#8

Well no, not limited only to database. Models can be used for other data storage beside database.

After validation, data should be as it is for insertion to a database yes. But there are other cases where you need to prepare the data even further. Example, user password change. If you are using PHP function password_hash for hashing passwords, you first have to retrieve the password from the database, to check the old password with password_verify. And then if the old password is correct, and the new passwords are confirmed to be the same(through validation), you need to update the user record. I would do that sort of thing in the model. Because I could then just copy the model to a new project, and already have all the functionality for password changing, all I would need to do, is a new view and controller method that calls the model method for password changing.
I also tend to do all the data validation in a model, yes, it makes the model depend on the form validation library, but it has a dependency for CI database library before that, so I don't see a bad thing here, because moving a model to a different framework will include much of overwrite anyway. The validation rules can be however set from a controller, if there is a need to, but I have a default set of rules defined in the model. Or skip the whole validation, if the situation requires it.
To minimize the code even further, and not have to rewrite this over and over again from model to model or app to app, I use a base model in a combination with a base controller. A simple form insert/update requires me to write almost no code, all I need to define, is a controller, not even a method, because a default one is defined in the base controller, and validation rules in the model, or in the controller method should I wish to define it.

I also tend to retrieve the whole POST/GET array at once in the controller, with
Code:
$this->input->post(); // or ->get();
To remove multiple function call overhead, and then proceed doing everything in the array.
Reply


Messages In This Thread
Model and Form - by aurelien - 11-20-2014, 03:24 AM
RE: Model and Form - by Rufnex - 11-20-2014, 04:10 AM
RE: Model and Form - by ivantcholakov - 11-20-2014, 05:00 AM
RE: Model and Form - by bclinton - 11-20-2014, 07:59 AM
RE: Model and Form - by slax0r - 11-20-2014, 04:11 PM
RE: Model and Form - by RobertSF - 11-20-2014, 05:59 PM
RE: Model and Form - by bclinton - 11-20-2014, 07:26 PM
RE: Model and Form - by slax0r - 11-21-2014, 01:43 AM
RE: Model and Form - by bclinton - 12-12-2014, 04:58 PM
RE: Model and Form - by mwhitney - 12-12-2014, 03:21 PM
RE: Model and Form - by bclinton - 12-12-2014, 04:55 PM
RE: Model and Form - by mwhitney - 12-15-2014, 10:02 AM
RE: Model and Form - by jaynarayan - 12-18-2015, 10:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB