php - 如何在 Codeigniter 上使用 $query->row 获取类对象

标签 php codeigniter object stdclass

我目前正在使用 Codeigniter 框架。在下面的代码中,我想得到一个 Animal_model 对象,而不是一个 stdClass 对象。

<?php
class Animal_model extends CI_Model{

    var $idanimal;
    var $name;
    public static $table = 'animals';

    function __construct() {
        parent::__construct();
    }

    function getone(self $animal){
        $query = $this->db->get_where(self::$table, array('id_animal' => $animal->idanimal));

        if($query == FALSE){
            return FALSE;
        }else{
            return $query->row(); // How to get an Animal_model object here? 
        }
    }
}

$lion = new Animal_model();
$lion->idanimal = 25; 
var_dump($lion); // It says "object(Animal_model)".

$lion = $lion->getone($lion);
var_dump($lion); // Now it says "object(stdClass)".

?>

如何将 $query->row() 转换为 Animal_model 对象?

最佳答案

CodeIgniter 有这方面的内置功能!

return $query->row(0, 'Animal_model');

关于php - 如何在 Codeigniter 上使用 $query->row 获取类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13916194/

相关文章:

php查询函数重复数据

php - 对 PHP func_num_args() 函数感到困惑

php - 在 PHP 中使用带有 group_concat 的对象

php - 当 C 使用 system() 和用户输入时,以 root 身份运行 C 是否危险?

php - 找不到 php_sqlsrv 扩展 - 5.3,线程安全

mysql - Codeigniter 大查询结果 result_array() result

codeigniter - 在 CodeIgniter 中将数据导出为 CSV

php - 在 Codeigniter 中添加语言的 URI 段

JavaScript : total length of an object

linux - 如何从文件中确定优化级别?