CodeIgniter Forums

Full Version: Model concept question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I have two controllers that have two models which have the exact same method which performs the exact same queries.


PHP Code:
// model 1
$this->model1->get_users();
// model 2
$this->model2->get_user(); 


It felt so wrong to have the same exact code that does the exact same thing in my models. What should I do.
Are you executing the same query in get_users() and get_user() method?
(01-15-2017, 10:27 PM)venkat Wrote: [ -> ]Are you executing the same query in get_users() and get_user() method?

Yes!!
(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?

Yes!!

Then you can place one method in either model or model2 and use that same method in two controllers.
(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?

Yes!!

Then you can place one method in either model or model2 and use that same method in two controllers.

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?

Yes!!

Then you can place one method in either model or model2 and use that same method in two controllers.

Doing that could confuse my pretend fellow developers, there must be an another way!


   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?

Yes!!

Then you can place one method in either model or model2 and use that same method in two controllers.

Doing that could confuse my pretend fellow developers, there must be an another way!


   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

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.
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 {
 
  //... your code here


With minimal setup, you can use the built-in functions of MY_Model.
Example:
PHP Code:
$this->load->model('user_model');
$this->user_model->where('city','London');
$this->user_model->order_by('address','ASC');
$users $this->user_model->get_all(); 

It even works with method chaining:
PHP Code:
$users $this->user_model->where('city','London')->order_by('address''ASC')->get_all(); 
Now, users is an array of objects with all the records that meet the 'where city = "London"' condition, in order of address. Easy as can be!
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.
Pages: 1 2