CodeIgniter Forums
Model concept question - 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: Model concept question (/showthread.php?tid=67100)

Pages: 1 2


Model concept question - meSmashsta - 01-15-2017

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.


RE: Model concept question - venkat - 01-15-2017

Are you executing the same query in get_users() and get_user() method?


RE: Model concept question - meSmashsta - 01-15-2017

(01-15-2017, 10:27 PM)venkat Wrote: Are you executing the same query in get_users() and get_user() method?

Yes!!


RE: Model concept question - venkat - 01-16-2017

(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.


RE: Model concept question - meSmashsta - 01-16-2017

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


RE: Model concept question - venkat - 01-16-2017

(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/10021481/can-one-call-a-method-from-another-model-in-a-model-in-code-igniter


RE: Model concept question - meSmashsta - 01-16-2017

(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/10021481/can-one-call-a-method-from-another-model-in-a-model-in-code-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


RE: Model concept question - InsiteFX - 01-16-2017

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.


RE: Model concept question - Wouter60 - 01-16-2017

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!


RE: Model concept question - meSmashsta - 01-17-2017

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.