I am using CodeIgniter 3, I've seen similar problems on this website and tried every permutation of the solutions offered to no avail.
I am trying to create a table via the cli with CodeIgniter. But this results in an error I just cant seem to figure out, any help is greatly appreciated!
Model:
<?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');
}
}
?>
Controller:
<?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();
}
}
?>
Terminal:
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:
As said in the comments : Your controller's file name must start with an uppercase. In your case, App_model.php. See http://codeigniter.com/userguide3/changelog.html
changed filenaming convention (class file names now must be Ucfirst and everything else in lowercase).
So change app_model.php
to App_model.php
EDIT :
Also, In your controller, Watch the caps : $this->load->model('app_model')
and $this->app_model->...
. Don't use any caps unless it's in your filenames.
Remove the closing php tags.
And in the end, you have a typo function __contruct
instead of function __construct()
In Codignitor 3
you need to use first letter capital for your file like:
app_model.php
Should be:
App_model.php
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.