How to access CodeIgniter user defined models?

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

When I want to access a function of a user defined model in CodeIgniter inside a custom user defined library it throws

Call to a member function Set_where() on a non-object

Although I load the model by using this inside that library

$CI =& get_instance();  $CI->load->model('home_model'); 

And I'm using this code to access the function inside home_model class

$CI->home_model->Set_where("film_feature='Y'"); 

So now it throws the above error.

So could someone please help me solve this error?

回答1:

You could try to instantiate your model object like:

include_once('path/modelname.php'); $home_model = new Home_model(); $home_model->set_where(....); 

make sure your model extends de default CI model class.

You could also use $CI->load->model(modelname); instead of include... It's easier to deal with path problems, however it would be a little less efficient because load->model will instantiate an object.



回答2:

Now i fix the issue just adding $CI =& get_instance(); into that function where i want to fetch previously i declare that $CI =& get_instance(); in another function of that class for which it was not instantiated.

My working code is now..

$CI =& get_instance();

$CI->load->model('home_model');

$CI->home_model->Set_where("film_feature='Y'");



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!