Zend_Db_Table_Row is a class that contains an individual row of a Zend_Db_Table object. When you run a query against a Table class, the result is returned in a set of Zend_Db_Table_Row objects. You can also use this object to create new rows and add them to the database table.
Zend_Db_Table_Row is an implementation of the Row Data Gateway pattern.
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.
Przykład 9.55. Example of fetching a row
<?php $bugs = new Bugs(); $row = $bugs->fetchRow('bug_id = 1'); ?>
A Zend_Db_Table_Rowset object contains a collection of Zend_Db_Table_Row objects. See Sekcja 9.6, „Zend_Db_Table_Rowset”.
Przykład 9.56. Example of reading a row in a rowset
<?php $bugs = new Bugs(); $rowset = $bugs->fetchAll("bug_status = 'NEW'"); $row = $rowset->current(); ?>
Zend_Db_Table_Row_Abstract provides accessor methods so you can reference columns in the row as object properties.
Przykład 9.57. Example of reading a column in a row
<?php $bugs = new Bugs(); $row = $bugs->fetchRow('bug_id = 1'); // Echo the value of the bug_description column echo $row->bug_description; ?>
![]() |
Notatka |
---|---|
Earlier versions of Zend_Db_Table_Row mapped these column accessors to the database column names using a string transformation called inflection. Current usage of Zend_Db_Table_Row does not implement inflection. The column accessor you use must match the spelling of the column name as it appears in your database. |
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 colum names
to the column values.
Przykład 9.58. Example of using the toArray() method
<?php $bugs = new Bugs(); $row = $bugs->fetchRow('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 Zend_Db_Table_Row_Abstract class provides methods for fetching rows and rowsets from related tables. See Sekcja 9.7, „Zend_Db_Table Relationships” for more information on table relationships.
You can set individual columns using column accessors, similarly to reading columns as object properties.
This changes the column value of the row as it exists
in your application, but it does not commit the change
to the database yet. You can do that with the
save()
method.
Przykład 9.59. Example of changing a column in a row
<?php $bugs = new Bugs(); $row = $bugs->fetchRow('bug_id = 1'); // Change the value of one or more columns $row->bug_status = 'FIXED'; // UPDATE the row in the database with new values $row->save(); ?>
![]() |
Notatka |
---|---|
Currently Zend_Db_Table_Row_Abstract throws an exception if you try to set the value of the primary key column(s) in the row. This is intended to change in Zend Framework 1.0. The Row class should permit the primary key columns to be set, to support sequences and natural primary keys. |
You can create a new row for a given table with the
fetchNew()
method of the table class.
Przykład 9.60. Example of creating a new row for a table
<?php $bugs = new Bugs(); $newRow = $bugs->fetchNew(); // Set column values as appropriate for your application $newRow->bug_description = '...description...'; $newRow->bug_status = 'NEW'; // INSERT the new row to the database $newRow->save(); ?>
Zend_Db_Table_Row_Abstract provides the
setFromArray()
method to enable you to set several
columns at once, specified in an associative array
mapping column names to values.
You may find this method convenient for setting values
both for new rows and for rows you need to update.
Przykład 9.61. Example of using setFromArray() to set values in a new Row
<?php $bugs = new Bugs(); $newRow = $bugs->fetchNew(); // Data are arranged in an associative array $data = array( 'bug_description' => '...description...', 'bug_status' => 'NEW' ); // Set all the column values at once $newRow->setFromArray($data); // INSERT the new row to the database $newRow->save(); ?>
You can call the delete()
method on a Row
object. This deletes rows in the database matching the
primary key in the Row object.
Przykład 9.62. Example of deleting a row
<?php $bugs = new Bugs(); $row = $bugs->fetchRow('bug_id = 1'); // DELETE this row $row->delete(); ?>
You do not have to call save()
to apply
the delete; it is executed against the database immediately.
It is often convenient to save the contents of a database row to be used later. Serialization is the name for the operation to convert an object into a form that is easy to save in offline storage (for example, a file). Objects of type Zend_Db_Table_Row_Abstract are serializable.
To be written.
To be written.
To be written.
Przykład 9.65. Example of reactivating a row
<?php $rowClone = unserialize($serializedRow); $bugs = new Bugs(); // Reconnect the row to a table, and // thus to a live database connection $rowClone->setTable($bugs); // Now you can make changes to the row and save them $rowClone->bug_status = 'FIXED'; $rowClone->save(); ?>
Some people prefer that the table class name match a table name in the RDBMS by using a string transformation called inflection.
Zend_Db classes do not implement inflection by default. See Sekcja 9.4.11.4, „Define Inflection in Zend_Db_Table” for a description of this design policy.
If you prefer to use inflection, then you must implement
the transformation yourself, by overriding the
_transformColumn()
method in a custom Row class,
and using that custom Row class when you perform queries
against your Table class.