Recently I was working on developing an application in PHP using Zend Framework, and when I tried to do an action that used the database in an previously serialized object that extends the class Zend_Db_Table_Row_Abstract – stored in a session, for example – an exception was thrown with the following message:
Cannot save a row unless it is connected
A small snippet of code that represents the problem:
$namespace = new Zend_Session_Namespace('signinData');
$user = $namespace->user;
$user->save();
The solution is simple and is in the documentation of the method setTable() in Zend_Db_Table_Row_Abstract class:
Set the table object, to re-establish a live connection to the database for a row that has been de-serialized.
What should be done is to call the object setTable() method by passing a new instance of the table to which the object refers.
Then the solution based on initial example:
$namespace = new Zend_Session_Namespace('signinData');
$user = $namespace->user;
$user->setTable(new App_Model_UserTable());
$user->save();
Thus, the exception ceases to be thrown when I try to save the user.
Tags: development, solution, zend framework
I’m having a problem related to this one too, except that after I call the setTable() method my whole row becomes null! It’s driving me nuts.
In fact, I CAN save() after the object is deserialized even though the documentation an everybody else says otherwise!