I have the following code:
class Badge extends CI_Model
{
public function foo()
{
echo $this->bar('world');
}
public function bar($word)
{
return $word;
}
}
But it always gives me an error on the line where echo $this->bar('world');
is.
Call to undefined method (......)
Yes, you can call a method from another model in a model in Code Igniter. You need the model you are calling a method on to be loaded. If you autoload all your models, it will always work.
Create a new User. php file in application/controllers/ directory. Load Model_3 Model and call fun1() and fun2() methods. Print the response.
Your not loading your model inside your controller:
public function test()
{
$this->load->model('badge');
$this->badge->foo();
}
Because the code works - I've just tested it by pasting using your model unedited:
class Badge extends CI_Model
{
public function foo()
{
echo $this->bar('world');
}
public function bar($word)
{
return $word;
}
}
output:
world
In order to avoid any external call dependecy you need to get the Codeigniter instance and call the method through the instance.
class Badge extends CI_Model
{
public function foo()
{
$CI =& get_instance();
echo $CI->Badge->bar('world');
}
public function bar($word)
{
return $word;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With