Welcome Guest, Not a member yet? Register   Sign In
help...can't load another model in a model
#1

[eluser]m4d3 Gun[/eluser]
hi..need help please... "can't load another model in a model"

i' using module system "Modular extention" in my blog,
and i have one module call "blog", inside..
- models : mdl_blog, mdl_categories
- controllers : blog
my problem is...when i call mdl_categories in mdl_blog to proccess in controller..
i got message : "unable to locate model....mdl_categories"

anyone can help me please....how to fix it?

thank's

M4d3
#2

[eluser]whitey5759[/eluser]
CI certainly allows you to load Models from inside other Models, so I suspect there is just a mistake in your code. Can you post your code?
#3

[eluser]m4d3 Gun[/eluser]
[quote author="whitey5759" date="1256554312"]CI certainly allows you to load Models from inside other Models, so I suspect there is just a mistake in your code. Can you post your code?[/quote]


thank's for ur replay Smile

this is my code :

in my controller like bellow...
Code:
<?

class Blog extends Controller{
        protected $_template;
        
    public function __construct(){
        parent::Controller();
        
        $this->load->model('mdl_blog');
    }
    
    
    public function index($page = NULL){
        
            $this->load->library('pagination');
            
            $config['total_rows'] = $this->mdl_blog->get_posts_count();
            $config['per_page'] = $this->mdl_blog->get_posts_per_page();
            $this->pagination->initialize($config);
            
            $pages_count = ceil($config['total_rows'] / $config['per_page']);
            $page = ($page == 0) ? 1 : $page;
            $offset = $config['per_page'] * ($page - 1);
            
        if($data['posts'] = $this->mdl_blog->get_posts($config['per_page'],$offset)){
            
            $data['posts_per_page'] = $this->mdl_blog->get_posts_per_page();
            $data['posts_count'] = $this->mdl_blog->get_posts_count();
            $data['pages_count'] = $pages_count;
            $data['current_page'] = $page;
            $data['next_page'] = $page + 1;
            $data['previous_page'] = $page - 1;
            
                foreach ($data['posts'] as $key => $post)
            {
                $data['posts'][$key]['url'] = post_url($post['url_title'], $post['date_posted']);
                //$data['posts'][$key]['display_name'] = $this->users->get_user_display_name($post['author']);
            }

            $this->_template['page']    = 'blog/posts';
         }
        else
        {
             $this->_template['page']    = 'errors/no_posts';
        }
            
        $this->system_library->load_template($this->_template['page'], $data);
    }
    
}
?>


mdl_blog
Code:
<?

class Mdl_blog extends Model{

    public function __construct(){
        parent::Model();
        
        $this->load->model('mdl_categories');
    }
    
    public function get_posts_per_page(){
        $this->db->select('value');
        $this->db->where('name','posts_per_page');
        $q = $this->db->get('settings',1);
        if($q->num_rows()== 1){
            $row = $q->row_array();
        }
        return $row['value'];
    }
    
    
    public function get_posts_count(){
        $this->db->select('id');
        $this->db->where('status','published');
        $q = $this->db->count_all_results('posts');
        
        return $q;
    }
    
    public function get_post_categories($post_id){
        $this->db->select('category.id');
        $this->db->where('post_id',$post_id);
        $q = $this->db->get('post2cat');
        if($q->num_rows()>0){
            $result = $q->result_array();
            foreach($result as $category){
                $categories[] = $category['category_id'];
            }
            return $categories;
        }        
    }
    
    function get_posts($number = 5, $offset = 0){
        $current_date = date('Y-m-d');
        
        $this->db->select('posts.id, posts.author, posts.date_posted, posts.title, posts.url_title, posts.excerpt, posts.content, posts.allow_comments, posts.sticky, posts.status, posts.author, users.display_name');
        $this->db->from('posts');
        $this->db->join('users','posts.author = users.id');
        $this->db->where('posts.status','published');
        $this->db->where('posts.date_posted <=' .$current_date);
        $this->db->order_by('sticky','DESC');
        $this->db->order_by('id','DESC');
        
        $q = $this->db->get();
        if($q->num_rows()>0){
            $result = $q->result_array();
            foreach(array_keys($result)as $key){
                $result[$key]['categories'] = $this->mdl_categories->get_categories_by_id($this->get_post_categories($result[$key]['id']));
                
            }
            return $result;
        }
    }
}
?&gt;


and mdl_categories
Code:
&lt;?

class Mdl_categories extends Model{

    public function __construct(){
        parent::Model();
    }
    
    public function get_categories(){
        $this->db->select('id,name,url_name,description');
        $q = $this->db->get('categories');
        if($q->num_rows()>0){
            return $q->result_array();
        }
    }
    
    public function get_categories_by_id($cat_id){
        $this->db->select('id,name,url_name,description');
        $this->db->where_in('id',$cat_id);
        $q = $this->db->get('categories');
        if($q->num_rows()>0){
            return $q->result_array();
        }
    }
    
    public function get_categories_by_post($post_id){
        $this->db->select('categories.name');
        $this->db->join('post2cat','categories.id = post2cat.category_id');
        $this->db->where('post_id',$post_id);
        $q = $this->db->get('post2cat');
        if($q->num_rows()>0){
            return $q->result_array();
        }
    }
}
?&gt;
#4

[eluser]whitey5759[/eluser]
Ok I believe your issue is because you need to use the CI super object is order to call the loader. An example of how to do this:

$CI =& get_instance();
$CI->load->model("mdl_categories");




Theme © iAndrew 2016 - Forum software by © MyBB