Model concept question |
I have two controllers that have two models which have the exact same method which performs the exact same queries.
PHP Code: // model 1 It felt so wrong to have the same exact code that does the exact same thing in my models. What should I do.
(01-16-2017, 12:40 AM)venkat Wrote:(01-15-2017, 10:40 PM)meSmashsta Wrote:(01-15-2017, 10:27 PM)venkat Wrote: Are you executing the same query in get_users() and get_user() method? Doing that could confuse my pretend fellow developers, there must be an another way! (01-16-2017, 01:09 AM)meSmashsta Wrote:(01-16-2017, 12:40 AM)venkat Wrote:(01-15-2017, 10:40 PM)meSmashsta Wrote:(01-15-2017, 10:27 PM)venkat Wrote: Are you executing the same query in get_users() and get_user() method? Then you can write the query in one model and use that same method in another model. hope this below link is useful http://stackoverflow.com/questions/10021...de-igniter
(01-16-2017, 02:00 AM)venkat Wrote:(01-16-2017, 01:09 AM)meSmashsta Wrote:(01-16-2017, 12:40 AM)venkat Wrote:(01-15-2017, 10:40 PM)meSmashsta Wrote:(01-15-2017, 10:27 PM)venkat Wrote: Are you executing the same query in get_users() and get_user() method? Never mind that link, I got it now! I'll use composition! This is a perfect candidate for an oop principle called composition, wohoo! This is the first time I'm actually going to use a technique like this XD
You can also create your own MY_Model and extend it to all of your other models.
Place code in iot that all your controllers would use those methods. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Why re-invent the wheel?
Avenirer has developed a great MY_Model class: https://github.com/avenirer/CodeIgniter-MY_Model You must put the MY_Model.php file in the application/core folder. Then, create your own model in application/models and extend the MY_Model class. Like this: PHP Code: class User_model extends MY_Model { With minimal setup, you can use the built-in functions of MY_Model. Example: PHP Code: $this->load->model('user_model'); It even works with method chaining: PHP Code: $users = $this->user_model->where('city','London')->order_by('address', 'ASC')->get_all();
Thanks for your suggestions guys, but I'm already fine with my implementations plus I have a golden thumb to not touch the core apis.
|
Welcome Guest, Not a member yet? Register Sign In |