user3282275
user3282275

Reputation: 1

Codeigniter model not loading with no errors

My Codeigniter model is not loading and i get no errors. When i try to use $this->load->model('All_sensor_data'). Nothing Happens. I have no idea what is going on. I have tried changing names of files, Class Names, etc..

BTW, that log message is never reached. I can remove the $this->load->model('all_sensor_data') and then the log message works.

My Base Model:

<?php
//Filename: application/core/my_model.php
class MY_Model extends CI_Model {
    const DB_TABLE = 'abstract';
    const DB_TABLE_PK = 'abstract';

    /**
     * Create record.
     */
    private function insert() {
        $this->db->insert($this::DB_TABLE, $this);
        $this->{$this::DB_TABLE_PK} = $this->db->insert_id();
    }

    /**
     * Update record.
     */
    private function update() {
        $this->db->update($this::DB_TABLE, $this, $this::DB_TABLE_PK);
    }

    /**
     * Populate from an array or standard class.
     * @param mixed $row
     */
    public function populate($row) {
        foreach ($row as $key => $value) {
            $this->$key = $value;
        }
    }

    /**
     * Load from the database.
     * @param int $id
     */
    public function load($id) {
        $query = $this->db->get_where($this::DB_TABLE, array(
                $this::DB_TABLE_PK => $id,
            ));
        $this->populate($query->row());
    }

    /**
     * Delete the current record.
     */
    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});
    }

    /**
     * Save the record.
     */
    public function save() {
        if (isset($this->{$this::DB_TABLE_PK})) {
            $this->update();
        }
        else {
            $this->insert();
        }
    }

    /**
     * Get an array of Models with an optional limit, offset.
     *
     * @param int $limit Optional.
     * @param int $offset Optional; if set, requires $limit.
     * @return array Models populated by database, keyed by PK.
     */
    public function get($limit = 0, $offset = 0) {
        if ($limit) {
            $query = $this->db->get($this::DB_TABLE, $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;
    }
} 

My Model:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//filename: application/models/all_sensor_data.php
class All_sensor_data extends MY_Model {

    const DB_TABLE = "all_sensor_data";
    const DB_TABLE_PK = 'id';

    /**
     * All_Sensor_Data primary key
     * @var int
     */
    public $id;

    /**
     * Sensor Name
     * @var string
     */
    public $name;

    /**
     * Defines the type of sensor. e.g. LIGHT, MOISTURE, TEMPERATURE
     * @var string
     */
    public $type;

    /**
     * Date the row was added
     * @var
     */
    public $modify_date;

    /**
     * Value read from the sensor
     * @var int
     */
    public $value;

    /**
     * Describes the section where the sensor is located.
     * @var
     */
    public $section;

}

Here is my controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: James
 * Date: 6/24/14
 * Time: 9:25 PM
 */
class Incoming extends CI_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->model('All_sensor_data');
    }
    public function index() {
       echo "Hello, Incoming Controller";
    }

    public function incomingSensorData($sensor, $value)
    {
        log_message('info', __CLASS__ . ' ' . __LINE__ . ' ' .  'Hello Incoming Data');
    }

}

Log output:

DEBUG - 2014-07-07 22:59:14 --> Hooks Class Initialized
DEBUG - 2014-07-07 22:59:14 --> Utf8 Class Initialized
DEBUG - 2014-07-07 22:59:14 --> UTF-8 Support Enabled
DEBUG - 2014-07-07 22:59:14 --> URI Class Initialized
DEBUG - 2014-07-07 22:59:14 --> Router Class Initialized
DEBUG - 2014-07-07 22:59:14 --> Output Class Initialized
DEBUG - 2014-07-07 22:59:14 --> Security Class Initialized
DEBUG - 2014-07-07 22:59:14 --> Input Class Initialized
DEBUG - 2014-07-07 22:59:14 --> Global POST and COOKIE data sanitized
DEBUG - 2014-07-07 22:59:14 --> Language Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Config Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Hooks Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Utf8 Class Initialized
DEBUG - 2014-07-07 22:59:49 --> UTF-8 Support Enabled
DEBUG - 2014-07-07 22:59:49 --> URI Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Router Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Output Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Security Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Input Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Global POST and COOKIE data sanitized
DEBUG - 2014-07-07 22:59:49 --> Language Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Loader Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Helper loaded: url_helper
DEBUG - 2014-07-07 22:59:49 --> Database Driver Class Initialized
DEBUG - 2014-07-07 22:59:49 --> Controller Class Initialized
DEBUG - 2014-07-07 22:59:49 --> CI_Model Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Config Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Hooks Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Utf8 Class Initialized
DEBUG - 2014-07-07 23:00:50 --> UTF-8 Support Enabled
DEBUG - 2014-07-07 23:00:50 --> URI Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Router Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Output Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Security Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Input Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Global POST and COOKIE data sanitized
DEBUG - 2014-07-07 23:00:50 --> Language Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Loader Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Helper loaded: url_helper
DEBUG - 2014-07-07 23:00:50 --> Database Driver Class Initialized
DEBUG - 2014-07-07 23:00:50 --> Controller Class Initialized
DEBUG - 2014-07-07 23:00:50 --> CI_Model Class Initialized

Upvotes: 0

Views: 1513

Answers (1)

Adeel Raza
Adeel Raza

Reputation: 636

Have your made your model properly , i.e

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class All_sensor_data extends CI_Model
{
 // Your functions here
}

and named your model as all_sensor_date.php and placed it in models folder in ci.

Now if you call it in your controller it will surely work.

Regarding the error reporting , go to your main index.php file, i.e in the same directory as your application folder in ci and check the environment which is defined , make sure it is set to development for reporting all errors.

EDIT :

there is an error here $this->load->model('All_sensor_data'); //You have used the class name here to load your model

Use this instead

$this->load->model('all_sensor_data'); //Filename is used when loading a model

Upvotes: 1

Related Questions