简体   繁体   English

如何在教义1.2中一一读取结果?

[英]How to fetch results one by one in Doctrine 1.2?

How can I make something similar to classic PHP PDO 我如何制作与经典PHP PDO类似的内容

while ($obj = $stmt->fetch()) {...}

in Doctrine 1.2. 在教义1.2中。

I've tried 我试过了

while ($obj = $query->fetchOne()) {...} 

but it returns always only the first object found. 但它始终仅返回找到的第一个对象。

Is there any way I could implement this behaviour? 有什么办法可以实现这种行为?

Doesn't really matter my query. 我的查询并不重要。 It's a plain simple one (note that I am in the *Table class of the model) 这是一个简单的例子(请注意,我在模型的* Table类中)

$query = $this->createQuery('a');

While working on a migration command in symfony2 I discovered that $query->executes(); 在symfony2中处理迁移命令时,我发现$query->executes(); executes $query->executes(); fetches the complete result set into RAM before returning it. 在将其返回之前将完整的结果集提取到RAM中。

This is not a problem with a small amount of data, but with bigger results your process might run OutOfMemory. 少量数据不是问题,但是结果较大,您的进程可能会运行OutOfMemory。

To solve the problem you can iterate through the results "one by one" with this statement: 要解决该问题,您可以使用以下语句“一个一个”地遍历结果:

$query->iterate();

I thing its similare to this PDO statement: while ($obj = $stmt->fetch()) {...} 我觉得它与该PDO语句相似: while ($obj = $stmt->fetch()) {...}

A more complete example might look like this: 一个更完整的示例可能如下所示:

$em = $this->getContainer()->get('doctrine');
$iterator = $em->getRepository('AcmeBundle:Entity')
               ->createQueryBuilder('e')
               ->getQuery()->iterate();

foreach($iterator as $item){
   $item = $item[0];

   // Your logic here:
   $item->getId();
   ....       
}

Found this in the doctrine documentation . 学说文档中找到了这一点

Have you tried: 你有没有尝试过:

while ($obj = $query->execute()) {...}

And if you want to fetch an array instead of objects: 如果要获取数组而不是对象:

while ($obj = $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY)) {...}

If you got the connection, do it like this: 如果您已建立连接,请按照以下步骤进行操作:

$stmt = $conn->query($queryString);
while ($obj = $stmt->fetch()) {...} 

With fetchOne() you will only get one entry at all. 使用fetchOne()您将仅获得一个条目。

Use the hydrate mode Doctrine_Core::HYDRATE_ON_DEMAND 使用水合物模式Doctrine_Core::HYDRATE_ON_DEMAND

$q = Doctrine_Query::create()
                ->select('...')
                ->from('...');

// Returns instance of Doctrine_Collection_OnDemand
$result = $q->execute(array(), Doctrine_Core::HYDRATE_ON_DEMAND);
foreach ($result as $obj)
{
    // do something with your object
}

More information : Data Hydrators — Doctrine 1.2.4 documentation 更多信息: 数据水合器-Doctrine 1.2.4文档

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

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