Doctrine迭代看起来很脆弱

时间:2012-05-27 08:44:43

标签: php doctrine-orm

正如我在another question中发现的那样,将300k行作为具有Doctrine的实体加载并不是一个好主意,并且至少会消耗一千兆字节的内存。

由于我还需要一些按位操作,我想我会在结果上使用NativeQueryiterate()。但是,在第一行之后,我的值始终为空。我的代码与the documentation高度相似:

$sql = "SELECT `pushToken` FROM `Devices`";
$rsm = new Doctrine\ORM\Query\ResultSetMappingBuilder(self::$entityManager);
$rsm->addScalarResult("pushToken", "pt");
$query = self::$entityManager->createNativeQuery($sql, $rsm);
$iterableDevices = $query->iterate();
foreach ($iterableDevices as $row)
{
    $message->addRecipient($row[0]["pt"]);
}

当我var_dump'编辑$row时,我注意到每次代码在循环中运行时索引实际上都会增加。这与文档相矛盾。我做错了什么,还是我发现了一个诚实的错误?

1 个答案:

答案 0 :(得分:0)

我在这里找到了解决方案 Merge all sub arrays into one 并且确实如此:

$iterator = $this->getEntityManager()->createNativeQuery($sql, $rsm)->iterate();
foreach ($iterator as $row) {
    yield call_user_func_array('array_merge', $row);
}