4

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.

markus
  • 40,136
  • 23
  • 97
  • 142

4 Answers4

6

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!

Ondrej Machulda
  • 998
  • 1
  • 12
  • 24
4

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

David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
  • I ended up doing what I thought I wasn't able too, meaning I integrated everything into the query. But thanks for the custom rowset input which I will certainly need at one point. – markus Apr 21 '09 at 07:50
1

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.

smack0007
  • 11,016
  • 7
  • 41
  • 48
-2

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

foreach ($rowset as $row)
{
    if (!$condition == satisfied)
    {
       $row->delete();
    }
}
gmcrist
  • 178
  • 3
  • 3
    delete is destructive. The row will be deleted from the table. – Darryl E. Clarke Apr 18 '09 at 18:24
  • maybe I wasn't clear but I don't want to delete the row from the table, I just want to remove it from the object!! – markus Apr 18 '09 at 20:11
  • Please accept my apologies for not understanding what you were wanting to do. As dcaunt suggested, and smack0007 affirmed, using a custom rowset would be the best option. – gmcrist Apr 20 '09 at 06:21