It is often convenient to save the contents of a database row to be used later.
Serialization is the name for the operation that converts 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.
Simply use PHP's serialize() function to
create a string containing a byte-stream representation of the Row object argument.
Example 305. Example of serializing a row
<?php
$bugs = new Bugs();
$row = $bugs->fetchRow('bug_id = 1');
// Convert object to serialized form
$serializedRow = serialize($row);
// Now you can write $serializedRow to a file, etc.
Use PHP's unserialize() function to
restore a string containing a byte-stream representation of an object. The function
returns the original object.
Note that the Row object returned is in a disconnected state. You can read the Row object and its properties, but you cannot change values in the Row or execute other methods that require a database connection (for example, queries against related tables).
Example 306. Example of unserializing a serialized row
<?php
$rowClone = unserialize($serializedRow);
// Now you can use object properties, but read-only
echo $rowClone->bug_description;
Why do Rows unserialize in a disconnected state?
A serialized object is a string that is readable to anyone who possesses it. It could be a security risk to store parameters such as database account and password in plain, unencrypted text in the serialized string. You would not want to store such data to a text file that is not protected, or send it in an email or other medium that is easily read by potential attackers. The reader of the serialized object should not be able to use it to gain access to your database without knowing valid credentials.
You can reactivate a disconnected Row, using the setTable()
method. The argument to this method is a valid object of type
Zend_Db_Table_Abstract, which you create. Creating a Table
object requires a live connection to the database, so by reassociating the Table
with the Row, the Row gains access to the database. Subsequently, you can change
values in the Row object and save the changes to the database.
Example 307. 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();




