using codeigniter PHP Fatal error: Class 'MY_Model' not found

13,209

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

the my_model.php should be renamed to MY_Model.php.

It should be a case-sensitivity issue. Class names must have the first letter capitalized with the rest of the name lowercase.

Share:
13,209

Related videos on Youtube

Felix
Author by

Felix

Updated on June 04, 2022

Comments

  • Felix
    Felix almost 2 years

    Now I am learning CodeIgniter_2.1.4 but I got a php error;

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

    <?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;
       }
    }
    

    and my domain model is :

    <?php
    
    class Publication extends MY_Model {
    
        const DB_TABLE = 'publications';
        const DB_TABLE_PK = 'publication_id';
    
        public $publication_id;
        public $publication_name;
    
    }
    

    well when I get model in my controller I got this php error:

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

    I have tried two hours finding the reason but failed ):

  • Felix
    Felix over 10 years
    After I add require,I got this error ):Message: require_once(my_model.php): failed to open stream: No such file or directory
  • Hashem Qolami
    Hashem Qolami over 10 years
    @Felix Are you developing on Windows? File names are case sensitive on Unix-based operation systems, but not on windows.
  • Dave
    Dave over 10 years
    where is the my_model file located in relationship to the publication model?
  • Hashem Qolami
    Hashem Qolami over 10 years
    @Felix, That's weird, because Windows or Mac servers treat file names as case-insensitive while Linux/Unix does as case-sensitive. If you don't have any case-sensitivity issue on CentOS, that's new to me.
  • Hashem Qolami
    Hashem Qolami over 10 years
    CodeIgniter loads all MY_* classes placed in application/core/ automatically. but the point is the classe names and files SHOULD start with MY_ prefix (actually it is configurable) and follow by a Capitalized name.