phpcodeigniteroopmodel

CodeIgniter - Call method inside a model?


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 (......)


Solution

  • 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