【问题标题】:Codeigniter undefined property, can't load model into controllerCodeigniter 未定义属性,无法将模型加载到控制器中
【发布时间】:2016-03-05 20:39:43
【问题描述】:

我正在使用 CodeIgniter 3,我在这个网站上看到过类似的问题,并尝试了所提供的解决方案的所有排列都无济于事。

我正在尝试使用 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:

【问题讨论】:

  • 自动加载的数据库?
  • 为什么?如果我有 $this->load->database();
  • 你的模型文件名是 ucfirst 吗? App_model.php 不是 app_model.php
  • OK 不需要...您的文件名以大写字母App_model.php 或小写字母app_letter.php 开头?
  • 实际上,here。 ;)

标签: php codeigniter


【解决方案1】:

正如 cmets 中所说:您的控制器的文件名必须以大写字母开头。在您的情况下,App_model.php。 见http://codeigniter.com/userguide3/changelog.html

更改了文件命名约定(类文件名现在必须是 Ucfirst 以及其他所有小写字母)。

所以把app_model.php改成App_model.php

编辑:

另外,在您的控制器中,观察大写字母:$this-&gt;load-&gt;model('app_model')$this-&gt;app_model-&gt;...。不要使用任何大写字母,除非它在您的文件名中。

删除关闭的 php 标签。

最后,你有一个错字function __contruct 而不是function __construct()

【讨论】:

  • i.imgur.com/0nmokxo.png,重命名错误仍然存​​在
  • @SergeyAndreev,在你的控制器中,调用$this-&gt;app_model-&gt;...。不要使用任何大写字母,除非它在您的文件名中。
  • @SergeyAndreev 我找到了!你是对的,这真的很愚蠢。你有一个错字:__contruct() 而不是__construct()。所以构造函数从未被调用过,模型也从未被加载。
  • 大声笑,当然是......好吧......我只是向自己保证我不是很聪明=)。谢谢你的帮助。
  • 干得好,兄弟我很忙,没有为你兄弟投票
【解决方案2】:

Codignitor 3 中,您需要为文件使用首字母大写,例如:

app_model.php

应该是:

App_model.php 

【讨论】:

  • i.imgur.com/0nmokxo.png,重命名错误仍然存​​在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
相关资源
最近更新 更多