PHP mysqli_result:将Traversable接口与fetch_object一起使用 [英] PHP mysqli_result: Use Traversable interface with fetch_object
问题描述
PHP mysqli_result类现在实现Traversable接口已有一段时间. 这提供了一些有趣的可能性,例如以下代码:
the PHP mysqli_result class implements the Traversable interface for some time now. This gives some interesting possibilities like the following piece of code:
<?php
$db = new mysqli( '...' );
$result = $db->query( '...' );
foreach( $result as $row ) {
//$row is an associative array containing the fetched values
}
现在,我非常喜欢这个,但是在我目前正在研究的库中,我想获取对象,而不是关联数组.我还希望能够将一个类传递给它,所以我可以用它创建一个简单的模型系统
Now, I like this pretty much, but in the library I am currently working on I would like to have objects fetched, not associative arrays. I also want to be able to pass a class to it, so I can create a simple model system with it
为此,我将其扩展为:
<?php
class mysqli_result_extension extends mysqli_result {
protected $result_class;
public function set_result_class( $class ) { $this->result_class = $class; }
public function function_to_overwrite() {
return $this->fetch_object( $this->result_class );
}
}
我的问题和对您的问题:
现在如何从 fetch_assoc 切换到 fetch_object ? 我想覆盖Traversable接口用来返回结果的方法,但它没有定义任何方法( http://php.net/manual/en/class.traversable.php )
How do I switch from fetch_assoc to fetch_object now? I would like to overwrite the method the Traversable interface uses in order to return the result, but it doesn't define any (http://php.net/manual/en/class.traversable.php)
所以我需要重写什么才能从fetch_assoc切换到fetch_object并能够将$ result_class传递给它,以便能够执行以下操作:
So what would I need to overwrite in order to switch from fetch_assoc to fetch_object and to be able to pass my $result_class to it so I am able to do something like this:
<?php
$db = new mysqli_extension( '...' );
$posts = $db->query( 'select * from posts' );
//$result is object of type mysqli_result_extension
$posts->set_result_class( 'post' );
foreach( $posts as $post ) {
//$post is an instance of class 'post'
}
我不想要肮脏,笨拙的东西,例如覆盖fetch_assoc并只返回其中的对象
I don't want something dirty, hacky like overwriting fetch_assoc and just returning the object in it
在此先感谢您的帮助和输入
Thanks in advance for your help and input
推荐答案
使用PDO完全可以轻松完成您想要的操作.只需设置PDO::ATTR_DEFAULT_FETCH_MODE
属性,当执行foreach循环时,它将进行适当的提取.
As I stated in the comments, it's easy to do exactly what you want with PDO. Just set the PDO::ATTR_DEFAULT_FETCH_MODE
attribute, and it will do the appropriate kind of fetch when you do the foreach loop.
您还可以设置PDO::ATTR_STATEMENT_CLASS
属性来定义要使用的类.
You can also set the PDO::ATTR_STATEMENT_CLASS
attribute to define the class to use.
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS);
$pdo->setAttribute(PDO::ATTR_STATEMENT_CLASS,array('classname',$constructorArgs));
请参见 http://php.net/manual/en/pdo.setattribute. php 了解更多信息.
使用该解决方案,您需要根据要实例化的类来更改每个查询的ATTR_STATEMENT_CLASS
属性.
With that solution, you'd need to change the ATTR_STATEMENT_CLASS
attribute for each query, depending on what class you want to instantiate.
或者,您可以将其设置为在查询中指定类名:
Alternatively, you can set it to specify the classname within the query:
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE);
$stmt = $db->query("SELECT 'classname', * FROM `table` WHERE `id` = 1;");
只需将相关的类名添加到查询中,作为要选择的第一个字段即可.
Simply add the relevant classname to your query as the first field to be selected.
查看有关此问题的答案之一在PHP中,如何设置默认的PDO提取类?,以了解更多信息.
See one of the answers on this related question In PHP, How do I set default PDO Fetch Class? for more on this.
这篇关于PHP mysqli_result:将Traversable接口与fetch_object一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!