【问题标题】:How to extend mysqli_result and add new method如何扩展 mysqli_result 并添加新方法
【发布时间】:2016-07-29 11:00:26
【问题描述】:

Mysqli_result 类具有fetch_all() 方法,以行数组的形式一次从结果集中返回多行(实际上这是二维数组,因为行已经是字段数组)。还有一个结果集有方法 fetch_object() 以对象的形式返回单行。

现在我想扩展 mysqli_result 类并添加新方法 fetch_objects()(复数),它将返回关联键为记录 ID [id=>Object()] 的对象关联数组。当然,我还需要扩展 mysqli 类并覆盖它的 query() 方法以返回我的扩展结果集。但我不明白 mysqli 和返回的 mysqli_result 之间的连接和查询参数的传输是如何进行的。

这应该这样工作:

$db=new ReallyMySQLi(...); //initiating my mysqli

$myrst=$db->query($SQL); //returned my extended result

//fetch_objects() returning associative array of objects
//with record ID as associative key
foreach($myrst->fetch_objects() as $id=>$object)
{
  echo 'Customer ID:'.$id; //or $object->id;
  echo 'Customer firstname:'.$object->firstname;
  echo 'Customer lastname:'.$object->lastname;
}

【问题讨论】:

    标签: php inheritance mysqli resultset


    【解决方案1】:

    我在mysqli_result 的 PHP 手册上找到了这个并做了一些修改。不过我没试过。

    class Database_MySQLi extends MySQLi
    {
        public function query($query)
        {
            $this->real_query($query);
            return new Database_MySQLi_Result($this);
        }
    }
    
    class Database_MySQLi_Result extends MySQLi_Result
    {
        public function fetch_objects()
        {
            $rows = array();
            while($row = $this->fetch_object())
            {
                $rows[$row->id] = $row;
            }
            return $rows;
        }
    }
    

    而用法是

    $db=new Database_MySQLi(...); //initiating my mysqli
    
    $myrst=$db->query($SQL); //returned my extended result
    
    //fetch_objects() returning associative array of objects
    //with record ID as associative key
    foreach($myrst->fetch_objects() as $id=>$object)
    {
      echo 'Customer ID:'.$id; //or $object->id;
      echo 'Customer firstname:'.$object->firstname;
      echo 'Customer lastname:'.$object->lastname;
    }
    

    【讨论】:

    • 真不好意思,我真的不知道我怎么没找到这个笔记(虽然花了一些时间搜索)。但是,我测试了这段代码,它可以工作!!!但我仍然不明白它是如何工作的!!! Database_MySQLi 中的$this->real_query($query) 指令是做什么的!?此外,当 mysqli 发出 return new Database_MySQLi_result($this) 时,它会实例化扩展的 mysqli_result 并将自己作为参数发送给 result,但 result 的构造函数没有参数!我还是一头雾水!
    • 我也不知道 real_query。我只是路过,做了一个小研究,看到了这个。只是觉得我可以分享。上帝拯救公共但未记录的变量和方法!也许他只是 var_dumped mysqli_result 类并发现了这些。
    • 这里使用real_query() 而不是query() 方法至关重要!我测试过,此代码不适用于$this->query($query)。 real_query 执行查询并将结果留在数据库中(未缓冲的结果 - 仅将指向它的指针存储在 mysqli 中),此结果应在之后使用或存储。
    • 是的,它也被记录在案了..php.net/manual/tr/mysqli.real-query.php。错过了。
    【解决方案2】:

    对不起,我两年没用 mysqli 作为 mysql 驱动了。但我为你提出了这个建议。

    你可以选择PDO,PDO作为多数据库驱动太酷了。

    和 laravel eloquent 一样,学说都以 PDO 为基础。

    $pdo = new PDO('sqlite:db.sqlite');
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-03
      • 1970-01-01
      • 2023-03-18
      • 2013-03-01
      • 2018-07-27
      • 2018-11-11
      • 1970-01-01
      相关资源
      最近更新 更多