2016-03-05 86 views
1

我使用的是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: 
+0

数据库加载到自动加载? – devpro

+0

为什么?如果我有$ this-> load-> database(); –

+0

你的模型文件名为ucfirst? 'App_model.php'不是'app_model.php' – AdrienXL

回答

2

正如评论说:你的控制器的文件名必须以大写开头。在你的情况下,App_model.php。 见http://codeigniter.com/userguide3/changelog.html

改变文件命名约定(现在的类文件名必须是Ucfirst ,一切都在小写其他)。

因此改变app_model.phpApp_model.php

编辑:

而且,在你的控制器,观看上限:$this->load->model('app_model')$this->app_model->...。不要使用任何大写,除非它在您的文件名中。

删除关闭php标签。

而在最后,你有一个错字function __contruct代替function __construct()

+0

i.imgur.com/0nmokxo.png,重命名错误仍然存​​在 –

+0

@SergeyAndreev,在你的控制器中,调用'$ this-> app_model - > ...'。不要使用任何大写,除非它在您的文件名中。 – AdrienXL

+0

http://i.imgur.com/HLMhi6a.png,尝试过,但在这里....仍然没有运气 –

1

Codignitor 3你需要使用的第一个字母大写为您的文件,如:

app_model.php 

应该是:

App_model.php 
+0

i.imgur.com/0nmokxo.png,重新命名的错误仍然存​​在 –

相关问题