tags:

views:

48

answers:

2

When i want to access a function of a user defined model in codeigniter inside a custom user defined library it throughs

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 im using this code to access the function inside home_model class

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

So now it throughs me the above error.

So could someone plz help me out to solve this error.

+1  A: 

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.

Pedro Gil
let me know if it worked
Pedro Gil
As I mentioned in the other post, for which this is a duplicate, there's nothing inherently wrong with what pravat231 is doing, so it's impossible to determine what's wrong without further information. I think using include statements over CI's native loader library is unnecessary if pravat's done everything correctly. The error is not with the loader function, but probably something somewhere else in his code that he hasn't posted.
treeface
Also, if you're using CI, the loader class is automatically instantiated, regardless of whether or not you autoload things. This happens on line 82 of the base Controller class: $this->load =
treeface
Pedro the code is working without any error but im not getting the exact result what i want.
pravat231
A: 

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'");

pravat231
@pravat231, we could've helped you out with this if you had been more descriptive in your initial post. It sounds like you don't quite understand variable scope in the context of PHP classes. For more info on that, see here: http://www.tuxradar.com/practicalphp/6/0/0
treeface
Ya im a newbie in this context thats why i need sometime to solve it but neways thanx for you quick response.
pravat231