【问题标题】:Codeigniter model not loading with no errorsCodeigniter 模型未加载且没有错误
【发布时间】:2014-07-08 03:03:52
【问题描述】:

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

顺便说一句,该日志消息永远不会到达。我可以删除 $this->load->model('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

【问题讨论】:

  • 如果没有错误,那你为什么说它没有加载?控制器即使使用模型也不显示
  • 你怎么知道它不工作?您是否尝试调用方法?反应如何?我所看到的只是您正在加载模型,但实际上并未使用它,例如$this-&gt;All_Sensor_data-&gt;doMyJob();
  • 永远无法到达日志消息。我可以注释掉 $this->load->model('All_sensor_data') 并且日志消息有效。

标签: php codeigniter model


【解决方案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 中的应用程序文件夹相同的目录并检查定义的环境,确保将其设置为 开发用于报告所有错误。

编辑:

这里有错误 $this-&gt;load-&gt;model('All_sensor_data'); //你已经使用了这里的类名来加载你的模型

改用这个

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

【讨论】:

  • 我正在尝试扩展 MY_Model 中的 CI_Model 以添加其他功能。我调整了原始帖子中的间距。也许你可以得到更好的照片。
  • 你是如何扩展 CI_Model 的,也展示一下。
  • 您是否将环境设置为 'development' 。你现在能看到任何错误吗? ?
  • &lt;?php //Filename: application/core/my_model.php class MY_Model extends CI_Model { const DB_TABLE = 'abstract'; const DB_TABLE_PK = 'abstract';
  • 没有错误。正如你所讨论的,我已经设置了开发环境。日志文件给了我上面添加的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 2012-03-04
相关资源
最近更新 更多