PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Retrieving Rows from a Rowset

The Rowset itself is usually less interesting than the Rows that it contains. This section illustrates how to get the Rows that comprise the Rowset.

A legitimate query returns zero rows when no rows in the database match the query conditions. Therefore, a Rowset object might contain zero Row objects. Since Zend_Db_Table_Rowset_Abstract implements the Countable interface, you can use count() to determine the number of Rows in the Rowset.

Example 320. Counting the Rows in a Rowset

<?php
$rowset   
$bugs->fetchAll("bug_status = 'FIXED'");

$rowCount count($rowset);

if (
$rowCount 0) {
    echo 
"found $rowCount rows";
} else {
    echo 
'no rows matched the query';
}

Example 321. Reading a Single Row from a Rowset

The simplest way to access a Row from a Rowset is to use the current() method. This is particularly appropriate when the Rowset contains exactly one Row.

<?php
$bugs   
= new Bugs();
$rowset $bugs->fetchAll("bug_id = 1");
$row    $rowset->current();

If the Rowset contains zero rows, current() returns PHP's NULL value.

Example 322. Iterating through a Rowset

Objects descending from Zend_Db_Table_Rowset_Abstract implement the SeekableIterator interface, which means you can loop through them using the foreach() construct. Each value you retrieve this way is a Zend_Db_Table_Row_Abstract object that corresponds to one record from the table.

<?php
$bugs 
= new Bugs();

// fetch all records from the table
$rowset $bugs->fetchAll();

foreach (
$rowset as $row) {

    
// output 'Zend_Db_Table_Row' or similar
    
echo get_class($row) . "\n";

    
// read a column in the row
    
$status $row->bug_status;

    
// modify a column in the current row
    
$row->assigned_to 'mmouse';

    
// write the change to the database
    
$row->save();
}

Example 323. Seeking to a known position into a Rowset

SeekableIterator allows you to seek to a position that you would like the iterator to jump to. Simply use the seek() method for that. Pass it an integer representing the number of the Row you would like your Rowset to point to next, don't forget that it starts with index 0. If the index is wrong, ie doesn't exist, an exception will be thrown. You should use count() to check the number of results before seeking to a position.

<?php
$bugs 
= new Bugs();

// fetch all records from the table
$rowset $bugs->fetchAll();

// takes the iterator to the 9th element (zero is one element) :
$rowset->seek(8);

// retrieve it
$row9 $rowset->current();

// and use it
$row9->assigned_to 'mmouse';
$row9->save();

getRow() allows you to get a specific row in the Rowset, knowing its position; don't forget however that positions start with index zero. The first parameter for getRow() is an integer for the position asked. The second optional parameter is a boolean; it tells the Rowset iterator if it must seek to that position in the same time, or not (default is FALSE). This method returns a Zend_Db_Table_Row object by default. If the position requested does not exist, an exception will be thrown. Here is an example:

<?php
$bugs 
= new Bugs();

// fetch all records from the table
$rowset $bugs->fetchAll();

// retrieve the 9th element immediately:
$row9 $rowset->getRow(8);

// and use it:
$row9->assigned_to 'mmouse';
$row9->save();

After you have access to an individual Row object, you can manipulate the Row using methods described in Zend_Db_Table_Row.

Zend Framework