首页 > 解决方案 > Codeigniter 加载错误的类

问题描述

我似乎无法加载application/model/Event.php模型类然后从中访问方法。
相反,CI 加载application/core/App_loader.php并尝试在那里寻找方法。

任何人都可以帮忙解决这个问题吗?

在:application/config/config.php

//$config['subclass_prefix'] = 'MY_';
$config['subclass_prefix'] = 'App_';

事件.php

class Event extends CI_Model {

  private $db_main;

  function __construct() {
    parent::__construct();
    $this->db_main = $this->load->database('main', TRUE);
  }

   function get($arr = array()) {
    // ! Trying to access this method ...
   }
}

我试图从控制器加载一个名为 Event 的模型类(验证函数 index() 被调用):application/controller/home.php

class Home extends App_Controller {

  private $event;

  function __construct() {
    parent::__construct();
    $this->event = $this->load->model('Event');
  }

  function index() {
    $this->method1();
  }

  function method1() {
     $eventArr = $this->event->get(); // << Cant access method
  }

Message: Call to undefined method App_Loader::get()

application/core/App_loader.php 里面

class App_Loader extends CI_Loader {
  function __construct() {
    parent::__construct();
  }

  function aa(){}
  function bb(){}
  function cc(){}
}

标签: phpcodeigniter

解决方案


参考来自https://www.codeigniter.com/userguide3/general/models.html#loading-a-model

class Event extends CI_Model {

    private $db_main;

    function __construct() {
        parent::__construct();
        $this->db_main = $this->load->database('main', TRUE);
    }

    function get($arr = array()) {
        // ! Trying to access this method ...
    }
}

class Home extends App_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('event');
    }

    function index() {
        $this->method1();
    }

    function method1() {
        $eventArr = $this->event->get();
    }
}

推荐阅读