use*_*600 7 php oop codeigniter
这些是控制器
class Dashboard extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model("admin/post_model");
$this->load->model("admin/comment_model");
}
public function index(){
$data['post_res'] = $this->post_model->getPost();
$data['com_res'] = $this->post_model->getComments();
}
}
Run Code Online (Sandbox Code Playgroud)
我无法在同一个控制器中加载2个模型.它给了我一个错误
Fatal error: Call to a member function getComments() on a non-object in C:\xampp\htdocs\blog\application\controllers\ram-admin\dashboard.php on line 13
Run Code Online (Sandbox Code Playgroud)
我怎么可能加载模型?
非常感谢你!
试试这个
class Dashboard extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model("admin/post_model","post_model");
$this->load->model("admin/comment_model","comment_model");
}
public function index(){
$data['post_res'] = $this->post_model->getPost();
$data['com_res'] = $this->comment_model->getComments();
}
Run Code Online (Sandbox Code Playgroud)
getComments( ) 是comment_model,而不是 post_model ..
您可以通过传递第二个参数来命名模型;
$this->load->model('admin/comment_model', 'comments');
$data['com_res'] = $this->comments->getComments();
Run Code Online (Sandbox Code Playgroud)