不确定措辞的最佳方式,所以请耐心等待。
在 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/