![]() |
Global method for all 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: Global method for all models (/showthread.php?tid=65136) |
Global method for all models - CINewb - 05-04-2016 I've created a customer model. One of the methods is called check_exists() and this takes a parameter (email address) and checks in the `customer` database table to see if the customer already exists. This method is called from the customer controller. Now I've created a user model, and created a new method within also called check_exists(). This takes a parameter (username) and checks in the `user` database table to see if the user already exists. This method is called from the user controller. For efficiency I am thinking I could create a global check_exists() function which accepts some additional parameters (database table and field name) and this could be used by all models. Where would be the place to put this? I had considered creating a library for it, but I did read that libraries should really be independent and not include database calls. Is this true? Is there a better place to put it? Or should I stick to creating multiple check_exists() methods in each model? Thanks, RE: Global method for all models - InsiteFX - 05-04-2016 See the CI Documentation and create a MY_Model.php RE: Global method for all models - CINewb - 05-04-2016 (05-04-2016, 03:44 AM)InsiteFX Wrote: See the CI Documentation and create a MY_Model.php Thank you, I understand where this is going now ![]() RE: Global method for all models - CINewb - 05-10-2016 Thanks, I extended CI_Model using MY_Model and included my method there RE: Global method for all models - InsiteFX - 05-10-2016 Your welcome |