Jun15
I recently ran into a problem using CodeIgniter where I kept receiving the error: “Call to a member function on a non-object”. This problem occurred when I was trying to use a method from one model from within another model.
I have a model named mGalleryImages. Within this model, I have a method named addGalleryImages. Within this method, I load another model and tried to call a method from the loaded model:
function addGalleryImages(){ ... $this->load->model('mUtilImages'); ... $this->mUtilImages->setImageFile($_FILES['uploadFile']); }
I receive the error when the setImageFile method is called. The problem is that when the model sees “$this”, it is looking for a method within the mGalleryImages model. The solution was to use a CodeIgniter function named get_instance().
function addGalleryImages(){ ... $CI =& get_instance(); $this->load->model('mUtilImages'); ... $CI->mUtilImages->setImageFile($_FILES['uploadFile']); }
Easy enough, but I would never have thought that this was the problem. I played with this code for hours, trying to figure out why it didn’t work similarly to a regular PHP class. But now I know.
Hi, I’ve try your solution to use $CI = & get_instance();
but it didn’t work too. Have you another solution?
Thanks
I need more information. Can u send me code snippets and let me know exactly where you receive the error?
I think there is a problem with your code…
function addGalleryImages(){
…
$CI =& get_instance();
$this->load->model(‘mUtilImages’);
…
$CI->mUtilImages->setImageFile($_FILES[‘uploadFile’]);
}
Shouldn’t it be:
$CI->load->model(‘mUtilImages’);
?
.-= John P.´s last blog ..Mouseterpiece Theatre =-.
I copied the code directly from a project that I worked on.
Hi,
I had exactly the same problem and this fixed it for me. Many thanks,
Andy
Hi,
I searched a solution for this problem all evening yeaterday and nothing :(. Today i again search and…. it’s work!!! Big big thank’s!:)
I’ve meet this problem this morning and through your solution I’ve helped much. Thanks.
This also works on view’s.
When loading a view in a helper function the same error rises. with this solution it should work.
I think it’s not a best practice, but it will do the job.
Hi All,
i am also facing the same problem and this solution works for me
Thanks
Hi All, I am Developing a web site. my below code work perfectly. but i have a problem i want “$statsid” above the second foreach, i want to pass this as variable or id to controller to get the data from table where equls to this “$statsid”
result() as $rows):?>
<img src="config->item(‘base_url’).$rows->profile_picture_path;?>” />
user_name;?> description;?>
<a href="#nogo" id="status_id;?>” class=”image_title” />Comments
status_id; ?>
result() as $rs):?>
<img src="config->item(‘base_url’).$rows->profile_picture_path;?>” />
user_name;?> status_comment;?>
Hi John,
Your solution worked for me. Instead of using
$this->load->model(‘myUtilImages’);
I have used the below and it works fine
$CI =& get_instance();
$CI->load->model(’mUtilImages’);
thanks a lot!
your solution help me much. thank you
Thank you man. I had a same problem. I was trying to load a model inside a view and then to load a method inside the model. It totally worked for me. Thanks a bunch!