Codeigniter模型没有加载没有错误

时间:2014-07-08 03:03:53

标签: php codeigniter model

我的Codeigniter模型未加载,我没有错误。 当我尝试使用$ this-> load->模型('All_sensor_data')时。什么都没发生。我不知道发生了什么事。我尝试过更改文件名,类名等。

顺便说一句,从未到达该日志消息。我可以删除$ this-> load->模型('all_sensor_data'),然后日志消息就可以了。

我的基本模型:

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

我的模特:

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

}

这是我的控制器:

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

}

日志输出:

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

1 个答案:

答案 0 :(得分:1)

你的模型是否正确,即

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

并将您的模型命名为all_sensor_date.php并将其放在ci中的模型文件夹中。

现在,如果你在你的控制器中调用它,它肯定会起作用。

关于错误报告,请转到主 index.php 文件,即与ci中的应用程序文件夹位于同一目录中并检查已定义的环境,确保将其设置为< b>开发报告所有错误。

编辑:

这里有一个错误  $this->load->model('All_sensor_data'); //您在此处使用了类名来加载模型

改为使用

$this->load->model('all_sensor_data'); //加载模型时使用文件名