• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Model concept question

#1
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.
Reply

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

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

Yes!!
Reply

#4
(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.
Reply

#5
(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!
Reply

#6
(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
Reply

#7
(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
Reply

#8
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 )
Reply

#9
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!
Reply

#10
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.