Welcome Guest, Not a member yet? Register   Sign In
accessing database functions in a helper
#1

[eluser]sunnyd[/eluser]
Is there a way I can access a database model from a helper? or is it even possible?
#2

[eluser]mddd[/eluser]
Sure. But to access "$this" in a helper you must load the CI object. A model has all the CI properties loaded automatically, while a helper does not.
Code:
$this->db->get('users');
then in a helper you should write
Code:
$CI = & get_instance();
$CI->db->get('users');
#3

[eluser]Twisted1919[/eluser]
You really don't have any reason to access the database from a helper .
If you do that it means you want to query in your views which is WRONG .
#4

[eluser]sunnyd[/eluser]
Thanks mddd,

I was on the right track, just did not instantiate $CI and was using $this instead. Everything is working the way I want it now Smile
#5

[eluser]WanWizard[/eluser]
Hmmm... Assumptions...

I can think of several valid reasons why you would want this, that have nothing to do with views.

I don't even use views, and still I have a database helper, that works around the fact that it's not easy to extend the database classes (working on this, but at the moment the helpers are still needed).
#6

[eluser]sunnyd[/eluser]
[quote author="Twisted1919" date="1279897092"]You really don't have any reason to access the database from a helper .
If you do that it means you want to query in your views which is WRONG .[/quote]

Well I am trying to add quick code snippets that can used across the website easily. I am making use of modules and dont always want to load up the models everytime. Basically, what I am trying to achieve is something like this..

Code:
function display_MonthlyArchives() {

        $CI =& get_instance();

        $CI->db->select('count(id_art) AS ArticleCount, date_art');
        $CI->db->group_by('EXTRACT(YEAR_MONTH FROM date_art)');
        $CI->db->order_by('date_art','DESC');
        $query = $CI->db->get('gk_blog_posts');
        if ($query->num_rows > 0) {
            $archives = $query->result();
            $query->free_result();

            $output = "<ul>";
            /* @var $archive blog_model */
            foreach ($archives as $archive) :
                $output .= "<li id='archive-".date("Ym", strtotime($archive->date_art))."'>";
                //$output .= anchor(base_url()."blog/archive/".date("Y-m", strtotime($archive->date_art)),date("F Y", strtotime($archive->date_art)));
                $output .= anchor(base_url()."blog/archive/".date("Y/m", strtotime($archive->date_art)),date("F Y", strtotime($archive->date_art)));
                $output .= " (".$archive->ArticleCount.")</li>";
            endforeach;
            $output .= "</ul>";
            return $output;
        } else {
            return array();
        }
}

I could have the above code in a model, but that would mean having to load the model in the modules, saving the result in a variable and displaying the view. Where as with the helper, I could just load the helper anywhere and simply call up the function which would return the results.
#7

[eluser]mddd[/eluser]
There can be perfectly legitimate reasons for accessing the database from a helper. I agree with WanWizard on that.
But I do think that the example sunnyd shows here is 'dangerous' in that it is taking functionality you would normally put in a model, and moving it to the view layer. Now, you have multiple places dealing with 'loading archives': probably in an archive model somewhere, AND in this helper!

I think the correct way would be to simplify the helper to something like:
Code:
function display_monthly_archives()
{
  $CI =& get_instance();
  $CI->load->model('archive_model');
  $archives = $CI->archive_model->get_archives();
  return $this->load->view('monthly_archives', $archives, true);
}
If you do that, you have the advantage of being able to call the helper function from anywhere, without having to load the model beforehand. But still you are keeping the 'smarts' in the model and the 'output bits' in a view. Much more maintainable.

But, in the end, everyone has to do it the way they like. That's the bottom line for me.
#8

[eluser]sunnyd[/eluser]
Thanks again,

mddd, I think your methods seems a lot safer and more customisable than mine. Had make a couple of modifications to your code in order to get it to work. Albeit, it requires the creation of a few more view files, this is definitely is a better approach.

thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB