[英]PDO fetch single class not working
i am making a database abstraction class that binds objects like an ORM.我正在制作一个数据库抽象类,它可以像 ORM 一样绑定对象。 I'm having issue with a particular case, fetching a single row and binding to a class.
我在特定情况下遇到问题,获取单行并绑定到一个类。 While the same is working well with fetchAll() i can't figure out why using fetch(PDO::FETCH_CLASS) the object returns null.
虽然同样适用于 fetchAll(),但我无法弄清楚为什么使用 fetch(PDO::FETCH_CLASS) 对象返回 null。 if i use PDO::FETCH_LAZY it works, but isn't a correct binding to the passed class.
如果我使用 PDO::FETCH_LAZY 它可以工作,但不是对传递的类的正确绑定。 Here the code.
这里是代码。 The Database() class is connects to db using PDO.
Database() 类使用 PDO 连接到 db。 Products() is a class made of public attributes with same name of tables.
Products() 是一个由具有相同表名的公共属性组成的类。 The controller:
控制器:
public function editProducts($params) {
$products = new Products();
$db = new Database ();
$id = array_keys($params);
$products = $db->findById($products, $id[0]); // auto Bind object fetched=no and POST params?
$this->template = new Template();
$this->template->renderArgs("product", $products);
$this->template->renderArgs("page_title", "Edit product " . $products->title);
$this->template->render(get_class($this), "editProducts");
}
The DB class:数据库类:
public function findById($object,$id) {
try {
$table = $this->objectInjector($object);
} catch (Exception $e) {
if (APP_DEBUG) {
d($e->getTrace());
}
return;
}
$statement = "SELECT * FROM $table WHERE id=:id";
$this->stm = $this->pdo->prepare($statement);
$this->bindValue(":id",$id);
return $this->fetchSingleObject($object);
}
the method that abstract fetch:抽象fetch的方法:
public function fetchSingleObject($object) {
$this->execute();
$this->stm->setFetchMode(PDO::FETCH_CLASS, get_class($object));
return $this->stm->fetch(PDO::FETCH_CLASS);
//return $this->stm->fetch(PDO::FETCH_LAZY); this works!
}
I missed something?我错过了什么? the fetchAll() works nicely in this way:
fetchAll() 以这种方式很好地工作:
public function fetchObjectSet($object) {
$this->execute();
$this->stm->setFetchMode(PDO::FETCH_CLASS, get_class($object));
return $this->stm->fetchAll(PDO::FETCH_CLASS);
}
Thank you so much.非常感谢。 PS: some methods like $this->execute() are just abastractions to pdo->statment method since pdo and stm are db class instance variables.
PS:像 $this->execute() 这样的一些方法只是对 pdo->statment 方法的抽象,因为 pdo 和 stm 是 db 类实例变量。
I found the answer to the question by myself, i post the answer for everyone.我自己找到了这个问题的答案,我把答案贴出来给大家。
Instead of using directly PDO::FETCH_CLASS, $Class, i switched using setFetchMode() passing PDO_FETCH_INTO, new $Object instance.我没有直接使用 PDO::FETCH_CLASS, $Class,而是使用 setFetchMode() 传递 PDO_FETCH_INTO,新的 $Object 实例。
This return correctly new instance of given object (with object methods and fields).这将正确返回给定对象的新实例(带有对象方法和字段)。 Works well with public attributes and overloaded constructors.
适用于公共属性和重载的构造函数。
The previously statement "findAll() works" wasn't true, i was returning somehow like FETCH_OBJ, an object representation of the database table.先前的语句“findAll() 工作”不是真的,我以某种方式返回了 FETCH_OBJ,这是数据库表的对象表示。
Here the solution:这里的解决方案:
public function fetchSingleObject($object) {
$this->stm->setFetchMode(PDO::FETCH_INTO, new $object());
$this->execute();
return $this->stm->fetch();
}
Return a new instance of passed in object.返回传入对象的新实例。
Works also as fetchAll()也可用作 fetchAll()
EDIT:编辑:
public function fetchObjectSet($object) {
$this->execute();
return $this->stm->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_class($object));
}
The manual states:该手册指出:
bool PDOStatement::setFetchMode ( int $PDO::FETCH_CLASS , string $classname , array $ctorargs )
bool PDOStatement::setFetchMode ( int $PDO::FETCH_CLASS , string $classname , array $ctorargs )
so perhaps try:所以也许尝试:
$this->stm->setFetchMode(PDO::FETCH_CLASS, 'get_class', $object );/* is $object an array ? */
or, without
$this->stm->setFetchMode(PDO::FETCH_CLASS, 'get_class' );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.