access model from view in codeigniter?

前端 未结 9 1305
星月不相逢
星月不相逢 2020-12-20 13:30

can anyone tell me how do i access model from view in codeigniter?

9条回答
  •  长情又很酷
    2020-12-20 13:57

    In cases when you want to access a model function from within a shared view , you don't have to load the needed model in every controller that will call that view. you can load the model inside the view itself by using the following code :

       $ci =&get_instance();
       $ci->load->model(model_name);
       $ci->model_name->function_name(); 
    

    in older versions of codeigniter the following code used to work :

    $this->load->model('model_name'); 
    model_name::function();   
    

    but when tested on CI 3.1.9 it throw the following error Message: Undefined property: CI_Loader::$model_name_model

    Note: I use this technique in template views (sidebar, menus ...etc) which is not used everywhere in my application , if you want to access a model from anywhere in your application considre loading this model globally by adding it to the autoload array in application/config/autoload.php

提交回复
热议问题