Zend Framework: Cannot save a row unless it is connected

11 May

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.

Advertisement

Tags: , ,

3 Responses to “Zend Framework: Cannot save a row unless it is connected”

  1. Julian September 22, 2010 at 15:09 #

    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!

Trackbacks/Pingbacks

  1. Tweets that mention Zend Framework: Cannot save a row unless it is connected « Gustavo Straube -- Topsy.com - May 11, 2010

    [...] This post was mentioned on Twitter by Gustavo Straube. Gustavo Straube said: Zend Framework's "Cannot save a row unless it is connected" solution: http://bit.ly/99IaCK #zf #php #in [...]

  2. Zend_Db_Table_Row and Zend_Cache | jameshd – web ramblings - September 27, 2010

    [...] objects should not have access to live database data as stated in the documentation. Credit also Gustavo he also has a simlar solution. This entry was posted in PHP, Zend Framework. Bookmark the [...]

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 )

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

Follow

Get every new post delivered to your Inbox.