Symfony and Saving Serialized Objects

Doctrine has a nice feature that allows users to save objects to a database. However, when using the “object” type in symfony, I discovered an annoying quirk; when updating a record with a serialized object, symfony does not update the object if the object reference has not changed. The following code will NOT work:

$record = Doctrine_Core::getTable('mytable')->find(array($id));
$object = $record->getObject();
$object->setName('newValue');
$record->setObject($object);
$record->save();

However, there is a work around. The following code will successfully update the record:

$record = Doctrine_Core::getTable('mytable')->find(array($id));
$object = $record->getObject();
$object->setName('newValue');
$record->setObject(new DummyObject());
$record->setObject($object);
$record->save();

By using a dummy object doctrine recognizes that the object has changed and will update the field upon save.

This entry was posted in Symfony and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>