Zend Framework:如何取消设置 Zend_Db_Table_Rowset 对象中的数据行

发布于 2024-07-17 11:55:28 字数 746 浏览 9 评论 0原文

我想迭代存储在 Zend_Db_Table_Rowset 对象中的数据行,然后删除/取消设置某些行(如果它们不满足某些条件)。

我可以使用 toArray() 只从对象中获取数据行,然后很容易取消设置我不需要的行。 但由于我想保留我的对象以供进一步使用,所以我不想这样做。

当然,一种解决方案是调整我的查询,以便仅检索我需要的内容,但在这种情况下这是不可能的。 至少我不知道怎么做。

我尝试了以下方法,但不起作用:

foreach ($rowset as $key => $row)
{
    if (!$condition == satisfied)
    {
        unset($rowset[$key]);
    }
}

当然它不起作用,因为没有 $rowset[$key]...数据存储在子数组 [_data:protected] 中,但未设置 $rowset[_data :protected][$key] 也不起作用。

也许我对行集对象(或一般对象的表示)的概念还不够成熟,无法理解我在做什么。 欢迎任何澄清和提示!

[编辑] $row->delete 不是一个选项,我不想从数据库中删除该行! 我不想先创建一个数组,如果我愿意,我只需执行 $rowset->toArray() [/编辑]

解决方案:我最终做了我认为自己无法做到的事情,这意味着我将所有内容都集成到了初始查询中。

I would like to iterate over the data rows stored in a Zend_Db_Table_Rowset object and then drop/unset some of the rows, if they don't fulfil certain criteria.

I could use toArray() to get only the data rows from the object and then it would be easy to unset the rows I don't need. But since I want to keep my object for further use I don't want to do that.

Of course one solution would be to adjust my query in order to retrieve only what I need, but that's not possible in this scenario. At least I wouldn't know how.

I tried the following which didn't work:

foreach ($rowset as $key => $row)
{
    if (!$condition == satisfied)
    {
        unset($rowset[$key]);
    }
}

And of course it doesn't work, since there is no $rowset[$key]... the data is stored in a subarray [_data:protected] but unset $rowset[_data:protected][$key] didn't work either.

Maybe my conception of a rowset object (or the representation of objects in general) is not mature enough to understand what I'm doing. Any clarification and tips would be welcome!

[EDIT]
$row->delete is NOT an option, I don't want to delete the row from the database!
I don't want to create an array first, if I wanted to I would just do $rowset->toArray()
[/EDIT]

Solution: I ended up doing what I thought I wasn't able too, meaning I integrated everything into the initial query.

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

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

发布评论

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

评论(4

自找没趣 2024-07-24 11:55:28

自定义行集类:

public function removeFromRowset($id) {
    foreach ($this->_rows as $i => $v) {
        if ($v->id == $id) { // found Row we want to remove
            unset($this->_rows[$i]); // delete both rowset
            unset($this->_data[$i]); // and original data
            break; // not necessary to iterate any more
        }
    }
    $this->_data = array_values($this->_data); // reindex both arrays (otherwise it breaks foreach etc.)
    $this->_rows = array_values($this->_rows);
    $this->_count = count($this->_data); // don't forget to update items count!
}

它迭代您的 Rowset,根据其 id(假设“id”是唯一标识符)找到 Row,然后将其从 Rowset 中删除。

请注意,这不会从数据库中删除该行,而只是将其从行集中删除!

Example method for your custom rowset class:

public function removeFromRowset($id) {
    foreach ($this->_rows as $i => $v) {
        if ($v->id == $id) { // found Row we want to remove
            unset($this->_rows[$i]); // delete both rowset
            unset($this->_data[$i]); // and original data
            break; // not necessary to iterate any more
        }
    }
    $this->_data = array_values($this->_data); // reindex both arrays (otherwise it breaks foreach etc.)
    $this->_rows = array_values($this->_rows);
    $this->_count = count($this->_data); // don't forget to update items count!
}

It iterate through your Rowset, finds Row according to its id (supposing "id" is unique identifier) and than remove it from the Rowset.

Please note this doesn't delete the row from database, it just removes it from the Rowset!

静谧 2024-07-24 11:55:28

您可以使用 自定义 Rowset 类

然后,该类将可以访问内部存储的受保护的 $_rows,并且您可以添加一个公共方法来应用过滤

You could use a custom Rowset class

This class would have then access to the protected $_rows stored internally, and you could add a public method to applying your filtering

尾戒 2024-07-24 11:55:28

我不知道有没有办法做到这一点。 您可以先创建一个数组,然后修改该数组。

$rows = array();
foreach($rowset as $row)
{
     $rows[$row->id] = $row;
}

foreach ($rows as $key => $row)
{
     if(!$condition == satisfied)
     {
         unset($rows[$key]);
     }
}

这不是最有效的做法,但确实有效。

I don't know that there is a way to do this. You could first create an array and then modify the array.

$rows = array();
foreach($rowset as $row)
{
     $rows[$row->id] = $row;
}

foreach ($rows as $key => $row)
{
     if(!$condition == satisfied)
     {
         unset($rows[$key]);
     }
}

Not the most efficient thing to do but it works.

千纸鹤 2024-07-24 11:55:28

您还可以对单个行对象使用delete() 方法。

foreach ($rowset as $row)
{
    if (!$condition == satisfied)
    {
       $row->delete();
    }
}

You can also use the delete() method on the individual row object.

foreach ($rowset as $row)
{
    if (!$condition == satisfied)
    {
       $row->delete();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文