我可以停止CakePHP获取查询的所有行吗? [英] Can I stop CakePHP fetching all rows for a query?

查看:104
本文介绍了我可以停止CakePHP获取查询的所有行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CakePHP与$ modelName-> find(...)
调用选择相当多的行(可能是数百)

I'm using CakePHP with $modelName->find(...) calls to select quite a number of rows (could be hundreds)

通常,在PHP / MySQL中,这当然不是一个问题,因为你在一个while循环中获取它们。
但是,CakePHP将所有行加载到一个数组中,该数组耗尽内存
limit。

Normally, in PHP/MySQL of course this wouldn't be a problem, as you fetch them in a while loop. But, CakePHP loads all rows in to an array which exhausts the memory limit.

有一种方法可以使用$ modelName-> find(...)构造,但是返回一个
迭代器来按需获取每一行?

Is there a way to use the $modelName->find(...) constructs but return an iterator to fetch each row on demand?

谢谢,
David

Thanks, David

推荐答案

不,开箱即用的Cake PHP(和一般的ActiveRecord)不支持迭代这样的结果集。如果你有一个用例,你实际需要所有表的记录,你可能最好使用原始SQL。 (或重新思考你的用例)。

No, out of the box Cake PHP (and ActiveRecord in general) doesn't support iterating over a result set like that. If you have a use case where you actually need all the records of a table, you're probably better off using raw SQL. (or rethinking your use case).

你也可以(并且可能已经考虑过)使用某种偏移,然后调用 - >找到多次。如果采用这种方法,不要忘记通过某个字段对结果集进行排序以确保确定性结果。当您离开ORDER BY时,数据库似乎只返回排序的行。我没有亲自尝试过,从多个查询的角度来看,它似乎效率低下,但它是值得尝试的。

You could also (and have likely already thought to) use an offset of some kind, and call ->find multiple times. If you take this approach don't forget to order your result set by some field to ensure a deterministic result. Databases only seem to return sorted rows when you leave an ORDER BY off. I haven't personally tried this, and it seems inefficient from a multiple queries standpoint, but it's something worth trying.

这篇关于我可以停止CakePHP获取查询的所有行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆