Welcome Guest, Not a member yet? Register   Sign In
Loading models in helpers.
#1

[eluser]ram4nd[/eluser]
I want to load a model in helper, but I have recursive function and it loads the model 100 times again and again if I load that page. How can I take the loading out of mu funcion.

My helper:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function get_main_category($category_id) {
    $CI = get_instance();
    $CI->load->model('categories_model');
    static $category, $up_category;
    //get category
    $category = $CI->categories_model->get_category($category_id);
    //get upper level category
    $up_category = $CI->categories_model->get_category($category->parent_id);
    //if upper category parent_id is 0, return category name
    if(empty($up_category->parent_id))
        return $category->name;
    //if upper category parent_id isn't 0, return get_main_category($category->parent_id)
    else
        return get_main_category($category->parent_id);
}

/* End of file categories_helper.php */
/* Location: ./system/application/helpers/categories_helper.php */
#2

[eluser]LifeSteala[/eluser]
For your situation, I highly suggest you autoload this model. Open ../application/config/autoload.php and add the model in there.
#3

[eluser]ram4nd[/eluser]
Ok, but maybe someone has better idea.
#4

[eluser]jalalski[/eluser]
Well, it should be:
$CI =& get_instance();
for a start...




Theme © iAndrew 2016 - Forum software by © MyBB