php - 使用 codeigniter PHP fatal error : Class 'MY_Model' not found

标签 php codeigniter codeigniter-2

现在我正在学习 CodeIgniter_2.1.4,但是我遇到了一个 php 错误;

我在/data/www/application/core 中有一个 my_model.php 文件

<?php

class MY_Model extends CI_Model {
   const DB_TABLE = 'abstract';
   const DB_TABLE_PK = 'abstract';

   private function insert() {
      $this->db->insert($this::DB_TABLE, $this);
      $this->{$this::DB_TABLE_PK} = $this->db->insert_id();
   }

   private function update() {
      $this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
   }

   public function populate($row) {
      foreach($row as $key => $value) {
         $this->$key = $value;
      }
   }

   public function load($id) {
      $query = $this->db->get_where($this::DB_TABLE, array(
         $this::DB_TABLE_PK => $id,
      ));
      $this->populate($query->row());
   }

   public function delete(){
      $this->db->delete($this::DB_TABLE, array(
         $this::DB_TABLE_PK => $this->{$this::DB_TABLE_PK},
      ));
      unset($this->{$this::DB_TABLE_PK});
   }

   public function save(){
      if(isset($this->{$this::DB_TABLE_PK})) {
         $this->update();
      }
      else {
         $this->insert();
      }
   }

 public function get($limit = 0, $offset = 0) {
      if($limit) {
          $query = $this->db->get($this::DB_TABE, $limit, $offset);
      }
      else {
          $query = $this->db->get($this::DB_TABLE);
      }

      $ret_val = array();
      $class = get_class($this);
      foreach ($query->result() as $row) {
          $model = new $class;
          $model->populate($row);
          $ret_val[$row->{$this::DB_TABLE_PK}] = $model;
      }
      return $ret_val;
   }
}

我的领域模型是:

<?php

class Publication extends MY_Model {

    const DB_TABLE = 'publications';
    const DB_TABLE_PK = 'publication_id';

    public $publication_id;
    public $publication_name;

}

好吧,当我在我的 Controller 中获取模型时,我得到了这个 php 错误:

PHP Fatal error:  Class 'MY_Model' not found in /data/www/application/models/publication.php on line 3

我已经尝试了两个小时来寻找原因但失败了):

最佳答案

I have a my_model.php file in /data/www/application/core

my_model.php 应该重命名为 MY_Model.php

应该是区分大小写的问题。类名的首字母必须大写,名称的其余部分小写。

关于php - 使用 codeigniter PHP fatal error : Class 'MY_Model' not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18189085/

相关文章:

javascript - 如何在单击嵌套在循环中的按钮时弹出显示模式

php - Symfony 强制登录并记住我

php - 如何在php中自动增加一个值?

php - 如何将 PHP 时区字符串(例如 Europe/Berlin)转换为 codeigniter 时区(例如 UP1)

php按值而不是引用复制数组元素

php - 是否可以使用或在 codeigniter 表单验证中?

php - 使用 Codeigniter 获取登录用户的所有消息以及每条消息的所有答案

php - 一个网站上有三个 Google Analytics 代码?

javascript - 使用提交按钮将 jquery 值传递给 php

mysql - 如何计算日期来制作事件列表?