Zendframework Rowset 通过键选择

发布于 2024-12-10 07:27:44 字数 104 浏览 1 评论 0原文

我试图通过行的主键访问 fetchAll 调用的行(返回 Zend_Db_Table_Rowset_Abstract)。

我想知道除了循环并找到所需的行之外,最简单的方法是什么。

I'm trying to access rows of a fetchAll call (returns Zend_Db_Table_Rowset_Abstract) by a row's primary key.

I was wondering what would be the easiest way of doing this, besides looping through and finding the desired row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

猥琐帝 2024-12-17 07:27:45

如果我没记错的话,Zend_Db_Table_Rowset_Abstract 只是传递了一个数组(或类似数组的东西),然后当您迭代它时,它会创建 Zend_Db_Table_Row_Abstract 对象。

最好的方法可能是循环一次,并将行存储在数组中,并按主键索引。这样你就可以执行一个循环,然后可以通过该键访问任何行。

更新:刚刚查看了源代码,这是(最终)传递给行集对象的数据:

$stmt = $this->_db->query($select);
$data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
return $data;

当您第一次迭代行集时,原始数组用于创建行对象。在此之前数据尚未循环,因此您没有执行已经完成的操作。

所以你必须做这样的事情来将行映射到主键(我有一段时间没有使用 Zend_Db_* ,将其视为伪代码):

$rows = array();
foreach($rowset as $row){
  //you could pull the primary key from Zend_Db_Table
  $rows[$row->id] = $row;
}

//now you can lookup by primary key
$rows[55]->name;

当然,你可以扩展抽象行集类并执行如果你愿意的话,可以在内部这样做。

If I recall correctly, Zend_Db_Table_Rowset_Abstract is just passed an array (or something that acts like one), then as you iterate through it, it creates the Zend_Db_Table_Row_Abstract objects.

The best way may be to loop through once, and store the rows in an array, indexed by the primary key. That way you do one loop, then can access any row by the key.

Update: Just took a look at the source, here's data that's (eventually) passed to the rowset object:

$stmt = $this->_db->query($select);
$data = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
return $data;

When you iterate through the rowset for the first time, that original array is used to create the row objects. The data hasn't been looped through before that point, so you're not doing something that's already been done.

So you have to do something like this to map rows to primary key (I haven't used Zend_Db_* for a while, treat this a pseudocode):

$rows = array();
foreach($rowset as $row){
  //you could pull the primary key from Zend_Db_Table
  $rows[$row->id] = $row;
}

//now you can lookup by primary key
$rows[55]->name;

Of course you can extend the abstract rowset class and do this internally if you want.

且行且努力 2024-12-17 07:27:45

使用 '$db->find($id)->current();'

希望有帮助。

Use '$db->find($id)->current();'

Hope it helps.

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