php - Codeigniter - 返回我的模型对象而不是 stdClass 对象

标签 php codeigniter codeigniter-2 models stdclass

不确定措辞的最佳方式,所以请耐心等待。

在 Codeigniter 中,我可以毫无问题地返回我的对象​​的记录集,但这作为 stdClass 对象返回,而不是作为“模型”对象(例如页面对象)返回,然后我可以使用它来使用其中的其他方法那个模型。

我是不是漏掉了什么技巧?或者这是 CI 中的标准功能?

最佳答案

是的,基本上为了让它工作,你需要在类范围内声明你的模型对象属性,并引用 $this 作为当前模型对象。

class Blogmodel extends CI_Model {

    var $title   = '';
    var $content = '';   // Declare Class wide Model properties
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function get_entry()
    {
        $query = $this->db->query('query to get single object');
        $db_row = $query->row();            //Get single record

        $this->title   = $db_row->title;
        $this->content = $db_row->content;  //Populate current instance of the Model
        $this->date    = $db_row->date;

        return $this;                       //Return the Model instance
    }
}

我相信 get_entry() 会返回一个对象类型 Blogmodel

关于php - Codeigniter - 返回我的模型对象而不是 stdClass 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5181963/

相关文章:

php - Codeigniter:使用数据库存储多个用户的数据库名称

php - 需要帮忙!我无法使用 codeigniter 在 oracle 11g 中连接我的程序

php - 如何扩展 CodeIgniter 数据库实用程序类

javascript - 使用 php 单击打开模态的 anchor 标记时获取模态上​​的 id

php - 跳过获取表中不需要的数据

php - 使用 Codeigniter 日期助手获取时区位置

mysql - 为什么 last_insert_id() 会在插入失败时返回一个值?

php - 运行 CodeIgniter 项目

php - 从 PHP 闭包中读取 "this"和 "use"参数

php - Android:使用 JOIN 查询使用 php 和 mysql 数据库填充 listview