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

Fetching a Row

Zend_Db_Table_Abstract provides methods find() and fetchAll(), which each return an object of type Zend_Db_Table_Rowset, and the method fetchRow(), which returns an object of type Zend_Db_Table_Row.

Example 302. Example of fetching a row

<?php
$bugs 
= new Bugs();
$row $bugs->fetchRow($bugs->select()->where('bug_id = ?'1));

A Zend_Db_Table_Rowset object contains a collection of Zend_Db_Table_Row objects. See the chapter about table rowset for details.

Example 303. Example of reading a row in a rowset

<?php
$bugs 
= new Bugs();
$rowset $bugs->fetchAll($bugs->select()->where('bug_status = ?'1));
$row $rowset->current();

Reading column values from a row

Zend_Db_Table_Row_Abstract provides accessor methods so you can reference columns in the row as object properties.

Example 304. Example of reading a column in a row

<?php
$bugs 
= new Bugs();
$row $bugs->fetchRow($bugs->select()->where('bug_id = ?'1));

// Echo the value of the bug_description column
echo $row->bug_description;

Note

Earlier versions of Zend_Db_Table_Row mapped these column accessors to the database column names using a string transformation called inflection.

Currently, Zend_Db_Table_Row does not implement inflection. Accessed property names need to match the spelling of the column names as they appear in your database.

Retrieving Row Data as an Array

You can access the row's data as an array using the toArray() method of the Row object. This returns an associative array of the column names to the column values.

Example 305. Example of using the toArray() method

<?php
$bugs 
= new Bugs();
$row $bugs->fetchRow($bugs->select()->where('bug_id = ?'1));

// Get the column/value associative array from the Row object
$rowArray $row->toArray();

// Now use it as a normal array
foreach ($rowArray as $column => $value) {
    echo 
"Column: $column\n";
    echo 
"Value:  $value\n";
}

The array returned from toArray() is not updateable. You can modify values in the array as you can with any array, but you cannot save changes to this array to the database directly.

Fetching data from related tables

The Zend_Db_Table_Row_Abstract class provides methods for fetching rows and rowsets from related tables. See the relationship chapter for more information on table relationships.

Zend Framework