CodeIgniter Forums
Using CRUD with OO Models - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Using CRUD with OO Models (/showthread.php?tid=62066)



Using CRUD with OO Models - Happy Camper - 06-06-2015

Hello All

I am following a tutorial at
http://www.revillweb.com/tutorials/codeigniter-tutorial-learn-codeigniter-in-40-minutes/

Could someone explain to me the best way to submit user data from the form to the database please?

At the moment this is what i do...
1. controller collects all user data and sends it to UserFactory as an array
2. UserFactory creates a new user object with the data it receives and returns the object to the controller
3. controller then calls commit method of the user object using $user->commit()
4. the commit method of the user model creates an array from the users attributes and then inserts this array into the database using
   $data = array(
       'username' => $this->_username,
       'password' => $this->_password
   );
 $this->db->insert("user", $data)


Is this the right way to do it? It seems excessive to create an object from an array, call the commit method of the object and then create another array from the objects private members which is finally inserted into the database.

I would love if you would comment and/or direct me to additional tutorials. PS I am using CI3

Thanks in advance


RE: Using CRUD with OO Models - ivantcholakov - 06-06-2015

The described in the tutorial way works, but it seems to me time-consuming. Every CRUD-operated table would require a model and a library. Adding a field to some table would require lots of modifications. I have a project with about 80 tables (although not all of them are CRUD-operated)... this approach is not for me.

For my purposes I use a base model https://github.com/ivantcholakov/codeigniter-base-model that is a modified and CI3-updated clone of Jamie Rumbelow's one https://github.com/jamierumbelow/codeigniter-base-model Unfortunately I can not provide a tutorial.
Edit: Anyway, see the trick "INPUT DATA FILTRATION BEFORE INSERT/UPDATE" in the home page of my project. It saves time.

A side note: The password field (from the tutorial) should be hashed.


RE: Using CRUD with OO Models - Happy Camper - 06-07-2015

Thanks ivan - some interesting reading here!