简体   繁体   English

PDO FetchAll Fetch_Class不返回所有行/对象

[英]PDO FetchAll Fetch_Class not returning all rows/objects

When using PDO::FetchAll() in association with PDO::Fetch_Class i don't get all objects back. 当与PDO::Fetch_Class一起使用PDO::FetchAll() ,我不会收回所有对象。 Just one object out of 5 or 6. 5或6中只有一个物体。

example: 例:

$as_object = 'Model_Menu'; $params = NULL;

$result->setFetchMode(PDO::FETCH_CLASS, $as_object, $params);

$result = $result->fetchAll();

But using $result->setFetchMode(PDO::FETCH_ASSOC); 但是使用$result->setFetchMode(PDO::FETCH_ASSOC); gives me all the rows in the database. 给了我数据库中的所有行。

Im using Kohana so i cant really show you the whole build of the query itself. 我正在使用Kohana,因此我无法真正向您展示查询本身的整个构建。 But trust me it works, if fetch assoc does get all the result with the same build it should work right? 但是,请相信我,它的工作原理是正确的,如果fetch assoc确实可以通过相同的构建获得所有结果,那它应该工作正常吗?

Environment : 环境 :

-Linux ubuntu
-Driver: pdo/mssql/?ORM?
-Framework: Kohana

Perhaps, because you're passing a third argument ( $params , which is null), PDO assumes the fetchmode to be PDO::FETCH_CLASS | PDO::FETCH_INTO 也许是因为您要传递第三个参数( $params ,它为null),所以PDO假定fetchmode为PDO::FETCH_CLASS | PDO::FETCH_INTO PDO::FETCH_CLASS | PDO::FETCH_INTO , re-assigning $params over and over. PDO::FETCH_CLASS | PDO::FETCH_INTOPDO::FETCH_CLASS | PDO::FETCH_INTO重新分配$params Remember that PDO::FETCH_CLASS will not call the constructor until after all properties are set. 请记住, PDO::FETCH_CLASS 不会调用构造函数,直到毕竟属性设置。 To call the constructor first, you need to set the fetch mode to PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE 要首先调用构造函数,您需要将提取模式设置为PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE . PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE Id' git this a bash: 我想这是一个bash:

$result->setFetchMode(
    PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE,
    $asObject
);
$objects = $result->fetchAll();

Looking at the docs, I've also noticed that most examples look like this: 在查看文档时,我还注意到大多数示例如下所示:

$stmt->execute(); //<-- execute
$rows = $stmt->fetchAll(PDO::FETCH_CLASS, 'ClassName');//pass fethcMode upon fetching

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM