Zend Framework: Cannot save a row unless it is connected

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;

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());

Thus, the exception ceases to be thrown when I try to save the user.


5 thoughts on “Zend Framework: Cannot save a row unless it is connected

  1. Hi! Thanks for the awesome post.
    In my case, I had a Row in session because every data in a whole module varies from this parent row.
    But in this row I had a user_id, and I stored in session with the user.name (from another table called `user`), being a column that doesn’t live in this table.
    The problem is: when I try to hydrate the object setting the table to find a dependent rowset, Zend Framework throws the Exception “The specified Table does not have the same columns as the Row”, as I don’t have a userName in the real table.

    Anyway, what I’m telling you guys, that just like me, stored a Row in session, is that you should better store a DTO or a ValueObject, and when you need it as a row, fetch it. Otherwise you will lose your whole morning as I did :/

    Again, thanks for the post.

  2. 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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.