Using codeigniter PHP Fatal error: class 'MY_Model' not found

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

I have 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:

 <?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 the model in the 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 tried for two hours to find the reason, but could not):

+4
source share
2 answers

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

my_model.php should be renamed to my_model.php .

This should be a case sensitivity case. Class names must have the first letter capitalized with the rest of the name in lowercase.

+13
source

in your publications.php has the following statement before the class declaration.

 require_once "my_model.php"; 

the error is that you did not include the definition of My_Model in your publications.php

-2
source

Source: https://habr.com/ru/post/1496605/


All Articles