Zend Framework: how to delete data rows in a Zend_Db_Table_Rowset object

I would like to iterate over the data rows stored in the Zend_Db_Table_Rowset object, and then discard / delete some of the rows if they do not meet certain criteria.

I could use toArray () to get only rows of data from the object, and then it would be easy to undo rows that I don't need. But since I want to save the object for future use, I do not want to do this.

Of course, one solution would be to tweak my query to get only what I need, but this is not possible in this scenario. At least I don’t know how.

I tried the following which did not work:

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

And, of course, this will not work, since there is no $ rowset [$ key] ... the data is stored in the [_data: protected] subarray, but unset $ rowset [_data: protected] [$ key] does not work either.

Perhaps my concept of a rowset object (or the representation of objects in general) is not mature enough to understand what I'm doing. Any clarifications and tips would be appreciated!

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

Solution . I ended up thinking that I also couldn’t, which means that I included everything in the original request.

+4
source share
4 answers

You can use your own Rowset class.

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

+4
source

Example method for a 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 iterates over your Rowset, finds the string according to its identifier (suppose "id" is a unique identifier), and then removes it from the Rowset string.

Note that this does not remove the row from the database; it simply removes it from the Rowset!

+6
source

I do not know that there is a way to do this. You can create an array first 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 effective thing, but it works.

+1
source

You can also use the delete () method for a single row object.

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

All Articles