Zend Framework:如何取消设置 Zend_Db_Table_Rowset 对象中的数据行
我想迭代存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自定义行集类:
它迭代您的 Rowset,根据其 id(假设“id”是唯一标识符)找到 Row,然后将其从 Rowset 中删除。
请注意,这不会从数据库中删除该行,而只是将其从行集中删除!
Example method for your custom rowset class:
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!
您可以使用 自定义 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
我不知道有没有办法做到这一点。 您可以先创建一个数组,然后修改该数组。
这不是最有效的做法,但确实有效。
I don't know that there is a way to do this. You could first create an array and then modify the array.
Not the most efficient thing to do but it works.
您还可以对单个行对象使用delete() 方法。
You can also use the delete() method on the individual row object.