代码点火器从另一个模型调用模型方法会导致错误


Codeigniter calling model method from another model causes error

我有一个控制器:

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

然后是bag_mode:

 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->bag_model->do_it()时,我得到一个错误:在非对象上调用成员函数do_it()

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

明白了!我不得不在blah_model构造函数中加载baga_model。这样就行了。

谢谢大家。

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();
       }
    }