PDO PHP 获取类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5137051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO PHP Fetch Class
提问by Aditya Shukla
I am learning pdo in php , so as to make database access easier and more efficient .One explanation i have read for fetch _class is that The properties of your object are set BEFORE the constructor is called.What does this mean? Any direction is greatly appreciated.
我正在 php 中学习 pdo,以便使数据库访问更容易和更高效。我读过的一个解释是 fetch _class 是在调用构造函数之前设置对象的属性。这是什么意思?任何方向都非常感谢。
回答by RobertPitt
This means that when using PDO to return a result into a custom object, you are required to set out the member variables which correspond to the query result keys.
这意味着在使用 PDO 将结果返回到自定义对象时,您需要设置与查询结果键对应的成员变量。
such as:
如:
class User
{
//Predefine Here
public $id;
public $username;
public $password;
public $email;
public $hash;
public function profileLink()
{
return sprintf('<a href="/profile/%s">%s</a>',$this->id,$this->username);
}
}
$result = $sth->fetchAll(PDO::FETCH_CLASS, "User");
foreach($result as $user)
{
echo $user->profileLink();
}
This way PDO can set the variables to the object outside of its internal scope.
通过这种方式,PDO 可以将变量设置为其内部范围之外的对象。
if you user class was like so:
如果你的用户类是这样的:
class User
{
}
then PDO Would not be able to set the values from outside the scope, as there are no properties defined.
那么 PDO 将无法从范围之外设置值,因为没有定义属性。
回答by Kris
Say you have this snippit of code
假设你有这段代码
<?php
class Foo {
public $bar;
public function __construct()
{
$this->bar = 1;
}
}
$stmt = $dbh->prepare("SELECT bar FROM foo");
$stmt->setFetchMode(PDO::FETCH_CLASS, 'Foo');
$stmt->execute();
$obj = $stmt->fetch()
?>
The bar propery for $obj will be set to "1" not what is retreived from the database.
$obj 的 bar 属性将设置为“1”,而不是从数据库中检索到的。
If you would like it to be set to the result from the database instead of "1" you can change the fetch mode to
如果您希望将其设置为来自数据库的结果而不是“1”,您可以将获取模式更改为
$stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Foo');
This causes the constructor to be called before assigning the results to the properties
这会导致在将结果分配给属性之前调用构造函数
回答by Juan Vilar
Looking back at PDO::FETCH_CLASS, you don't really need to define the variables as public nor private (as of PHP 7.0 ) , you could define an empty class and PHP PDO will populate the attributes as $Class->attribute even if they are not defined.
回顾 PDO::FETCH_CLASS,您实际上并不需要将变量定义为 public 或 private(从 PHP 7.0 开始),您可以定义一个空类,PHP PDO 会将属性填充为 $Class->attribute,即使它们没有定义。
this is very useful because you can reuse classes with multiple queries that treat the same table but might return different columns
这非常有用,因为您可以重用具有多个查询的类,这些查询处理同一个表但可能返回不同的列
回答by Halfacht
Instead of using: FetchAll(PDO::FETCH_CLASS, 'classname')
You can use: fetchObject('classname')
而不是使用:FetchAll(PDO::FETCH_CLASS, 'classname')
您可以使用:fetchObject('classname')
Example:
例子:
class Car extends DatabaseTable {
const TABLE_NAME = 'cars';
// Table Columns
private $color;
private $brand;
private $type;
public function __construct($pdo, $id = false)
{
parent::__construct($pdo, TABLE_NAME, $id);
}
public static function getAll($pdo, $order = 'id') {
$query = 'SELECT * FROM ' . self::TABLE_NAME . '
ORDER BY ' . $order;
$statement = $pdo->prepare($query);
$objects = [];
while ($obj = $statement->fetchObject(self::class, [$pdo])) {
$objects[$obj->getId()] = $obj;
}
return $objects;
}
The parent Constructor just sets its 3 properties like: $this->id = $id;
父构造函数仅设置其 3 个属性,例如: $this->id = $id;