我使用的是CodeIgniter 3,我在本網站上看到過類似的問題,並試圖對所提供的解決方案進行各種排列,但無濟於事。Codeigniter未定義屬性,無法將模型加載到控制器中
我想通過使用CodeIgniter的cli創建表。但是這導致了一個錯誤,我只是無法弄清楚,任何幫助都非常感謝!
型號:
<?php
class App_model extends CI_Model {
public function __construct(){
parent::__construct();
$this->load->database();
}
public function create_table(){
$this->drop();
$sql='CREATE TABLE app (
id int(11) NOT NULL AUTO_INCREMENT,
text text NOT NULL,
PRIMARY KEY (id)
);';
$this->db->query($sql);
}
public function drop(){
$this->db->query('DROP TABLE app');
}
}
?>
控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App extends CI_Controller {
public function __contruct(){
parent::__construct();
$this->load->model('App_model');
}
public function test(){
$this->App_model->create_table();
}
}
?>
終端:
asergey91:~/workspace $ php index.php App test
A PHP Error was encountered
Severity: Notice
Message: Undefined property: App::$App_model
Filename: /home/ubuntu/workspace/application/controllers/App.php
Line Number: 10
Backtrace:
File: /home/ubuntu/workspace/application/controllers/App.php
Line: 10
Function: _error_handler
File: /home/ubuntu/workspace/index.php
Line: 292
Function: require_once
Fatal error: Call to a member function create_table() on a non-object in /home/ubuntu/workspace/application/controllers/App.php on line 10
Call Stack:
0.0003 255192 1. {main}() /home/ubuntu/workspace/index.php:0
0.0010 317824 2. require_once('/home/ubuntu/workspace/system/core/CodeIgniter.php') /home/ubuntu/workspace/index.php:292
0.0171 1549776 3. call_user_func_array:{/home/ubuntu/workspace/system/core/CodeIgniter.php:514}() /home/ubuntu/workspace/system/core/CodeIgniter.php:514
0.0171 1550040 4. App->test() /home/ubuntu/workspace/system/core/CodeIgniter.php:514
A PHP Error was encountered
Severity: Error
Message: Call to a member function create_table() on a non-object
Filename: /home/ubuntu/workspace/application/controllers/App.php
Line Number: 10
Backtrace:
數據庫加載到自動加載? – devpro
爲什麼?如果我有$ this-> load-> database(); –
你的模型文件名爲ucfirst? 'App_model.php'不是'app_model.php' – AdrienXL