CodeIgniter Forums
Clarification on database access in helpers, models and plugins - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Clarification on database access in helpers, models and plugins (/showthread.php?tid=23052)



Clarification on database access in helpers, models and plugins - El Forum - 09-28-2009

[eluser]Ki[/eluser]
I wanted to ask for a clarification. I am not sure if I need to use model or plugin or helper for this. I am leaning towards a helper.
When users search for something on my website, they have many parameters to set for their search, such as price, category etc. I want to display the filters on the search page that are applied to current search. So I would create a function, that would read the URL as array in my controller, then it will load helper/model/plugin, which will help in filtering only search filters. For example, it will look for category_id/26 or price/150 in my url array. This helper/model/plugin will only take the url values I define as filters and strip away everything else.
The problem is that if url contains category/26, I dont want to display
Filters
category: 26
I want to display
category: parts

In doing so, whenever my my helper/model/plugin comes across category > 0 in url array, it will pull up the description from the database. Now, I could pass the category array to the helper, but since I have about 5-6 search filter that need description from db I dont want to pass 5-6 array every time.
As far as I know, helpers and plugins do not actyaly allow db acces, even if you extend Controller.
So should I put this into a model? or helper and set instance of CI?
Maybe I am way off and there is an easier way to do this?

I am trying to keep this db querying out of controller, so I dont have to pass unnecessary variables and have helper/model/plugin access db only when it detect url parameter that requires db access. I pre-define these parameters in array inside the helper/model/plugin as such:
$filters=array('category_id'=>array('function'=>'get_category_name','display_name'=>'category'));
Then I do array_intersect_key with url array and when it detects 'category' and sees that $filters=array['category_id'][function] != '' it calls the function defined in $filters=array['category_id'][function], which goes to db and gets the 'human' category name

Thank you in advance


Clarification on database access in helpers, models and plugins - El Forum - 09-29-2009

[eluser]gon[/eluser]
Hi,

You can access DB or any other CodeIgniter resource by getting a reference to the CI object:
$CI =& get_instance();
This object works the same way as the controllers. So, instead of $this->load->library(... you can do $CI->load->library(...
$CI->db is a reference to the DB object.
This way you can access DB from libraries, helpers or wherever you need.


Anyway I find your system a little overbloated, even if I don't understand it fully.
For searches I usually have a Search library. You should pass URL parameters to that library, and get search results. The library should convert names to ids, perform the searches, possibly using models for that, and return a nicely formatted array or object, ready to be passed to the views.


Clarification on database access in helpers, models and plugins - El Forum - 09-29-2009

[eluser]Ki[/eluser]
Gon,
Thanks for the reply - I agree with you, it is a bit overbloatd and I am trying to put it on a diet.
Can someone clarify the difference between Library, Helper, Model, Plugin in CI?
As far as I know:
Helper provides a group of functions
Model allows DB access and manipulation
Plugin is just one specific function
Where does the libary fits? How do you know when should you go with a combination of helpers and when to go with a library?


Clarification on database access in helpers, models and plugins - El Forum - 09-29-2009

[eluser]gon[/eluser]
Don't really have time for that!!!

Please try to get it for yourself and ask for more precise questions that may arise...

http://en.wikipedia.org/wiki/Model–view–controller


Clarification on database access in helpers, models and plugins - El Forum - 09-29-2009

[eluser]Ki[/eluser]
Gon,
With much respect and appreciation - your link did not provide any insight into my question: where does libary fits between helpers and models?
I understand that not everyone wants to bother or have time to assist in forums, but posting "I dont have time to answer look for yourself", is self contradictory, because you actually bothered to take time to answer... ;-)
Thanks anyway


Clarification on database access in helpers, models and plugins - El Forum - 09-29-2009

[eluser]Ki[/eluser]
By the way, for those who are confused whether they should build a library or helper, here is a great answer:
Clarifrication on usnig Libraries vs Helpers in CI


Clarification on database access in helpers, models and plugins - El Forum - 09-29-2009

[eluser]gon[/eluser]
Sorry if my answer was rude or you found it self-contradictory.
But I keep on the point: learn MVC, read some articles on MVC and PHP and you'll find Codeigniter very easy to work with.

Glad you found the answer to the question...