問題描述
我試圖弄清楚如何從 PDO 獲取到我的自定義類,以及一般的 PDO 到對(duì)象 API,但我發(fā)現(xiàn)缺乏體面的文檔令人沮喪.大多數(shù)選項(xiàng)僅作為選項(xiàng)記錄在案,而示例都使用提取到數(shù)組中.
那么,有人可以解釋一下這些是如何使用的:
I am trying to figure out how to fetch from PDO into my custom class, and in general the PDO-to-object API, and am finding the lack of decent documentation frustrating. Most of the options are only documented as an option, while the examples all use fetching into arrays.
So, can someone explain how these are used:
- PDO::FETCH_OBJ
- PDO::FETCH_CLASS
- PDO::FETCH_CLASSTYPE
- PDO::FETCH_INTO
- PDO::FETCH_LAZY
- PDOStatement::fetch
- PDOStatement::fetchObject
- PDOStatement::setFetchMode
如果可能的話,我想要一個(gè)一般性的解釋,說明每個(gè)函數(shù)/常量是如何用于獲取對(duì)象的,或者有什么區(qū)別,以及我如何獲取到我的類中的具體答案,例如進(jìn)入:
If possible, I would like a general explanation how each function/constant is used for fetching objects, or what the differences are, as well as a specific answer to how do I fetch into my class, e.g. into:
class MyRow {
public $col1, $col2;
}
推薦答案
這是我設(shè)法弄清楚的:
PDO::FETCH_OBJ
用于獲取未命名(匿名")對(duì)象的新實(shí)例
PDO::FETCH_OBJ
Is used to fetch into a new instance of an unnamed ("anonymous") object
PDO::FETCH_CLASS
用于獲取現(xiàn)有類的新實(shí)例(列名應(yīng)與現(xiàn)有屬性匹配,或者 __set
應(yīng)用于接受所有屬性).類的構(gòu)造函數(shù)將在屬性設(shè)置后調(diào)用.
PDO::FETCH_CLASS
Is used to fetch into a new instance of an existing class (the column names should match existing properties, or __set
should be used to accept all properties). The constructor of the class will be called after the properties are set.
PDO::FETCH_CLASSTYPE
與FETCH_CLASS
(按位或)一起使用,創(chuàng)建實(shí)例的類名在第一列,而不是提供給函數(shù).
PDO::FETCH_CLASSTYPE
Used with FETCH_CLASS
(bitwise OR), the name of the class to create an instance of is in the first column, instead of supplied to the function.
PDO::FETCH_INTO
用于與 FETCH_CLASS
相同類型的類(必須將所有列作為屬性名稱處理),但更新現(xiàn)有對(duì)象而不是創(chuàng)建新對(duì)象.
PDO::FETCH_INTO
Is used with the same type of class as FETCH_CLASS
(must handle all columns as property names), but updates an existing object as opposed to creating a new one.
PDO::FETCH_LAZY
我不知道這是做什么的.
PDO::FETCH_LAZY
I don't know what this does.
PDOStatement::fetch
常規(guī)的 get-a-row 命令.我不知道如何將它與FETCH_CLASS
或FETCH_INTO
一起使用,因?yàn)闆]有任何方法可以傳遞類名/實(shí)例.
PDOStatement::fetch
The regular get-a-row command. I don't know how to use this withFETCH_CLASS
orFETCH_INTO
since there does not be any way to pass the class name/instance.
PDOStatement::fetchObject
一種執(zhí)行 FETCH_CLASS
或 FETCH_INTO
的方法,包括傳遞構(gòu)造函數(shù)參數(shù).沒有參數(shù)是 FETCH_OBJ
的簡(jiǎn)寫.
PDOStatement::fetchObject
A way to do FETCH_CLASS
or FETCH_INTO
, including passing constructor args. With no args is a shorthand for FETCH_OBJ
.
PDOStatement::setFetchMode
一種設(shè)置默認(rèn)獲取模式的方法,以便可以在沒有參數(shù)的情況下調(diào)用 PDOStatment::fetch
.
PDOStatement::setFetchMode
A way to set the default fetch mode, so that PDOStatment::fetch
can be called without args.
這是我想出的最好的方法.我希望它可以幫助其他人(我需要 fetchObject
方法)
That is the best I managed to figure out. I hope it helps someone else (I needed the fetchObject
method)
這篇關(guān)于PHP PDO 獲取對(duì)象的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!