PHP PDO :: FETCH_LAZY-在访问对象变量名称时不创建它们 [英] PHP PDO::FETCH_LAZY - not creating object variable names as they are accessed

查看:96
本文介绍了PHP PDO :: FETCH_LAZY-在访问对象变量名称时不创建它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PDO,但无法理解PDO :: FETCH_LAZY.在PHP手册中,它说:"... PDO :: FETCH_LAZY在访问对象变量名称时创建...".我有以下代码对此进行了测试:

I was working with PDO, and could not understand PDO::FETCH_LAZY. In PHP manual it says "...PDO::FETCH_LAZY creates the object variable names as they are accessed...". I have this code to test this:


class b{
    function __construct(){}
}
$b = new b();

$pdo = new PDO('mysql:host=localhost;dbname=test', 'username', 'pssword');

//testtable{id int, title varchar, msg varchar, time varchar}

$res = $pdo->query("SELECT * FROM testtable limit 1");
$b = $res->fetch(PDO::FETCH_LAZY);
echo $b->msg;
var_dump($b);

这应该打印仅具有1个属性msg的对象b.但是,输出是这样的:

This is supposed to print the object b with only 1 property, msg. But instead, the output is this:


This is a sample message.

object(PDORow)#6 (5) {
  ["queryString"]=>
  string(31) "SELECT * FROM testtable limit 1"
  ["id"]=>
  string(1) "1"
  ["title"]=>
  string(5) "sample title"
  ["msg"]=>
  string(13) "This is a sample message."
  ["time"]=>
  string(7) "1232123"
}

任何人都可以对此发表一些看法吗?谢谢.

Can anyone please throw some light on this? Thanks.

推荐答案

PDORow是一个内部类,文档很少. 甚至不是手册中的 (搜索结果)

PDORow is an internal class that is very poorly documented. It's not even in the manual (search results).

查看PDORow的唯一方法是如果您使用FETCH_LAZY:

The only way to see a PDORow is if you FETCH_LAZY:

$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->bindValue(':id', 1, PDO::PARAM_INT);
$stmt->execute();
$lazy = $stmt->fetch(PDO::FETCH_LAZY);

有趣的是,它的所有属性都是公共的.第一个"queryString"与sql查询关联.这是某种代理.

The interesting thing is that all of its properties are public. The first one, 'queryString', is associated to the sql query. It is some kind of proxy.

您可以将其重新用于其他语句,以获取完整的对象. (几乎)获取PDORow不会增加内存使用量.它不可序列化,并且对其调用未定义的属性不会引发错误.

You can reuse it for further statements, to get the full object. Fetching a PDORow (almost) doesn't increase memory usage. It's not serializable, and calling an undefined property on it doesn't raise an error.

有关phpdelusions.net的更多信息

使用延迟获取时从PDO返回的对象不是stdclass对象,它们属于pdorow类型,并且具有一些神奇的功能,包括延迟的数据获取-这与pdo _驱动程序的设置方式有些混淆

Objects returned from PDO when fetch lazy is used are NOT stdclass objects, they are of type pdorow and have some magic features including delayed data fetching - this is a bit obscured with the way pdo _ drivers are set up

您可以关注这个有趣的讨论.

从php-src中,我们可以看到PDORow不是要实例化的

From the php-src, we can see that PDORow is not meant to be instantiated

您可以看到有关pdo_stmt.c中的类 Ctrl + F PDORow其余

You can see several details about the class in pdo_stmt.c Ctrl+F PDORow for the rest

修改

FETCH_LAZY的可能用例是:

A possible use case for FETCH_LAZY, would be:

// get the lazy object - aka, PDORow
    $stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
    $stmt->bindValue(':id', 100, PDO::PARAM_INT);
    $stmt->execute();
    $lazy = $stmt->fetch(PDO::FETCH_LAZY);

接下来,重新使用它:

// do whatever you please with the properties, since they're all public
   $publicProps = get_object_vars($lazy);

然后,如果需要,加载真实对象:

And then, load the real object if you need it:

// loading lazy object
    $stmt = $pdo->prepare($publicProps['queryString']);
    $stmt->bindValue(':id', $publicProps['id'], PDO::PARAM_INT); 
    /** you could use a regex to get an array of parameters' names prefixed by ':' in the original queryString
    * eg, $queryString = 'SELECT * FROM users WHERE firstName = :firstName AND email = :email';
    * preg_match_all('/:[^\s]+/', $queryString, $matches);
    * would give you an array with ":firstName" and ":email"
    */
    $stmt->execute([':id' => $publicProps['id']]);
    $notLazy = $stmt->fetch();

顺便说一句,(个人笔记)这越来越像教义代理了,所以马可!你最近好吗药膏,阿米科.

BTW, (personal note) this is beginning to look more and more like doctrine proxies, so hi Marco! How are you doing these days? Salve, amico.

这篇关于PHP PDO :: FETCH_LAZY-在访问对象变量名称时不创建它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆