Codeigniter 从另一个模型调用模型方法导致错误

Codeigniter calling model method from another model causes error

我有一个控制器:

class Blah extends Controller
{
     function Blah()
      {
        $this->load->model('baga_model');
      }
}

然后是 baga_model:

 class Baga_model extends Model
    {
      function do_it()
       {
         echo "BOOM!";
       }
    }

..和

class Blah_model extends Model
        {
          function some_action()
           {
             $this->baga_model->do_it();
           }
        }

所以 .. 当在 blah_model 我调用 $this->baga_model->do_it() 我得到一个错误: 在非对象上调用成员函数 do_it()

我只是不明白为什么....我知道它一定有效,我以前做过类似的事情.. 谢谢

public function test()
    {
        $this->load->model('baga_model');
        $this->baga_model->do_it();
    }

型号

class baga_model extends CI_Model
{
    public function do_it()
   {
     echo $this->bar("BOOM!");
   }

您没有在模型中加载所需的模型:

 class Blah_model extends CI_Model
    {
$this->baga_model = $this->load->model('baga_model', true);
        public function some_action()
       {
         $this->baga_model->do_it();
       }
    }

知道了!我必须在 blah_model 构造函数中加载 baga_model。这样就可以了。

谢谢大家