使用具体的 Zend_Db_Table_Abstract 有效地迭代行集
我正在使用 Zend_Db_Table_Abstract 的具体实现:
class DB_TestClass extends Zend_Db_Table_Abstract {
protected $_name = "test.TestData";
}
如果我想选择表中的所有行,我似乎有一个选择:
$t = new DB_TestClass;
$rowset = $t->fetchAll();
这返回 Zend_Db_Table_Rowset 的一个实例,它有一个可迭代的接口,您可以循环并将每个行条目作为 rowClass 访问实例:
foreach($rowset as $row) {
var_dump($row);
}
但是,行集已将数据库中的每一行加载到内存中(!)在小表上这是可以的,但在大表上(例如数千行)它很快耗尽 PHP 可用的内存并且脚本终止。
我如何使用 Zend_Db 迭代结果集,一次从语句句柄检索一行,ala mysql_fetch_assoc()?这将允许有效访问任意数量的行(数千、数百万),而无需使用过多的内存。
预先感谢您的任何建议。
I'm using a concrete implementation of Zend_Db_Table_Abstract:
class DB_TestClass extends Zend_Db_Table_Abstract {
protected $_name = "test.TestData";
}
If I want select all rows in the table, I seem to have one option:
$t = new DB_TestClass;
$rowset = $t->fetchAll();
This returns an instance of Zend_Db_Table_Rowset which has an iterable interface you can loop though and access each row entry as a rowClass instance:
foreach($rowset as $row) {
var_dump($row);
}
HOWEVER, the rowset has loaded every row from the database into memory(!) On small tables this is OK, but on large tables - thousands of rows, for example - it quickly exhausts the memory available to PHP and the script dies.
How can I, using Zend_Db, iterate through the result set retrieving one row from a statement handle at a time, ala mysql_fetch_assoc()? This would allow efficient access to any number of rows (thousands, millions) without using excessive memory.
Thanks in advance for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么fetchAll()并不能达到你想要的效果。
您需要使用 Zend_Db_Select 获取所有记录,然后通过循环执行您想要的操作
Then fetchAll () is not capable of what you want.
You need to use Zend_Db_Select to get all the records and then do what you've intended through the loop
您能否指定将所有行一起提取的目的?因为如果您想对表中的所有行进行某种处理,那么您必须以任何方式将所有行放入内存中。另一方面,如果你想做一些像分页这样的事情,你总是可以使用 zend_pagination 对象,它只会获取有限的编号。一次行数。或者更好的是你可以设置偏移量和否。要在 fetchAll 函数本身中获取的行数。
Can you please specify the purpose for fetching all the rows together? Cause if you want to do some kind of processing on all the rows in the table then you any which ways have to get all the rows in to the memory. On the other hand if you want to do something like pagination, you can always use zend_pagination object which will just fetch the limited no. of rows at one time. Or better still you can set the offset and no. of rows to be fetched in the fetchAll function itself.