使用具体的 Zend_Db_Table_Abstract 有效地迭代行集

发布于 2024-11-17 06:28:56 字数 624 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水波映月 2024-11-24 06:28:56

那么fetchAll()并不能达到你想要的效果。
您需要使用 Zend_Db_Select 获取所有记录,然后通过循环执行您想要的操作

        $select = new Zend_Db_Select ($this->getFrontController()->getParam('bootstrap')->getResource ('Db'));
        $statement = $select->from ('verses')->query ();
        $statement->execute ();


        while ($row = $statement->fetch ())
        {
            // try something like that
            $db_row = new Zend_Db_Table_Row (array ('table' => $table, 'data' => $row));

            $db_row->text = $db_row->text . '_';
            $db_row->save ();

        }

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

        $select = new Zend_Db_Select ($this->getFrontController()->getParam('bootstrap')->getResource ('Db'));
        $statement = $select->from ('verses')->query ();
        $statement->execute ();


        while ($row = $statement->fetch ())
        {
            // try something like that
            $db_row = new Zend_Db_Table_Row (array ('table' => $table, 'data' => $row));

            $db_row->text = $db_row->text . '_';
            $db_row->save ();

        }
半枫 2024-11-24 06:28:56

您能否指定将所有行一起提取的目的?因为如果您想对表中的所有行进行某种处理,那么您必须以任何方式将所有行放入内存中。另一方面,如果你想做一些像分页这样的事情,你总是可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文