Forms and Databases (Best Practice) - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Forms and Databases (Best Practice) (/showthread.php?tid=16640) |
Forms and Databases (Best Practice) - El Forum - 03-12-2009 [eluser]thors1982[/eluser] So, I am fairly new to Codeigniter and am wondering what the best practice is. I see many examples of where they get the post... pass in the post to the model and store all the data. But I have a few extra fields in my form that are not supposed to be in the database. Such as how the information should be saved. Examples: Draft, Publish, Revert Basically what I have done is instead of sending all the post information to the model and storing it in the DB I call a function(in the controller) which just sets only the vars I need in a new object. Then I pass the new object to the model and store everything. Example: Code: $newpage->id = $this->input->post('id'); Is this a normal/good practice or should I be doing something else? The reason I ask is because if i add a new field to the form it has to be updated in several places. Thanks for the help in advance Forms and Databases (Best Practice) - El Forum - 03-12-2009 [eluser]Jay Logan[/eluser] Why not put the info in the model and just call to the model when it's time to post to DB? Code: $newpage->id = $this->input->post('id'); Forms and Databases (Best Practice) - El Forum - 03-13-2009 [eluser]Vicente Russo[/eluser] Hi, Thats what I do: Code: // Form sent But its possible to fill the $fieldnames array automatically, and if you put this on a model, ou just have to mess in one place Forms and Databases (Best Practice) - El Forum - 03-13-2009 [eluser]thors1982[/eluser] Thanks Vicente Russo I ended up taking your suggestion its basically the same thing just much more elegant and clean. :-) I don't know if I was looking for that or something more automatic. But this definitely feels/looks much better. And I did end up moving it all to my model as well. So thank you both for that. Forms and Databases (Best Practice) - El Forum - 03-13-2009 [eluser]jpeffer[/eluser] I generally use the field names defined within my form validator definition to extract only those fields from the POST. Something like the following: Code: // Remove keys from the registration_data array that were not validated $registration_data is set as the method parameter in my authentication lib. All I then have to do from the controller is simply pass the $_POST array.... done |