3.4. Zend_Db_Table_Row

3.4.1. Introduction

Zend_Db_Table_Row is a row data gateway for the Zend Framework. In general, you do not instantiate Zend_Db_Table_Row by itself; instead, you get a Zend_Db_Table_Row back as a return result from Zend_Db_Table::find() or Zend_Db_Table::fetchRow(). Once you have a Zend_Db_Table_Row, you can edit the record values (represented as class properties) and then save() the changes back to the originating table.

3.4.2. Fetching a Row

The first thing to do is instantiate a Zend_Db_Table class.

<?php
// set up an adapter
require_once 'Zend/Db.php';
$params = array (
    'host'     => '127.0.0.1',
    'username' => 'malory',
    'password' => '******',
    'dbname'   => 'camelot'
);

$db = Zend_Db::factory('PDO_MYSQL', $params);

// set the default adapter for all Zend_Db_Table objects
require_once 'Zend/Db/Table.php';
Zend_Db_Table::setDefaultAdapter($db);

// connect to a table in the database
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
?>
        

Next, retrieve one record from the database using Zend_Db_Table::find() with a single key, or by using Zend_Db_Table::fetchRow(). The returned result will be a Zend_Db_Table_Row object where each property in the object is a "camelCaps" name mapped to an "underscore_words" column name from the table. E.g., "first_name" in the table will become "firstName" in the object properties.

<?php
// fetch a record from the table as a Zend_Db_Table_Row object
$row = $table->fetchRow('first_name = "Robin"');

//
// $row is now a Zend_Db_Table_Row object with public properties
// that map to table columns:
//
// $row->id = '3'
// $row->nobleTitle = 'Sir'
// $row->firstName = 'Robin'
// $row->favoriteColor = 'yellow'
//

?>
        

3.4.3. Modifying Values

Modifying row values is very easy: just work with the object properties the way you would with any other object. Then you can save() the row back to the table when you are done.

<?php
// connect to a table in the database
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();

// fetch a record from the table as a Zend_Db_Table_Row object
$row = $table->fetchRow('first_name = "Robin"');

//
// $row is now a Zend_Db_Table_Row object() with public properties
// that map to table columns:
//
// $row->id = '3'
// $row->nobleTitle = 'Sir'
// $row->firstName = 'Robin'
// $row->favoriteColor = 'yellow'
//
// change the favorite color and save back to the table.
$row->favoriteColor = 'blue';'
$row->save();
?>
        

However, you are not allowed to change primary key values; if you do so, Zend_Db_Table_Row will throw an exception.

<?php
// connect to a table in the database
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();

// fetch a record from the table as a Zend_Db_Table_Row object
$row = $table->fetchRow('first_name = "Robin"');

// can we change the primary key "id"?
try {
    $row->id = 5;
    echo "We should not see this message, as an exception was thrown.";
} catch (Zend_Db_Table_RowException $e) {
    echo $e->getMessage();
}
?>