From 818008492c36250b993addca53243dd7855ba150 Mon Sep 17 00:00:00 2001 From: nightfreak Date: Tue, 7 Aug 2007 15:37:30 +0000 Subject: [PATCH] =When unserializing a serialized record the value of enum, array, object and gzip fields are restored + it is now possible to store an object of type Doctrine_Record in an object field --- lib/Doctrine/Record.php | 23 ++- tests/Record/SerializeUnserializeTestCase.php | 154 ++++++++++++++++++ tests/run.php | 1 + 3 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 tests/Record/SerializeUnserializeTestCase.php diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index da28de8d8..1131f5ec5 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -498,7 +498,7 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count $this->_data = array_merge($this->_data, $this->_id); foreach ($this->_data as $k => $v) { - if ($v instanceof Doctrine_Record) { + if ($v instanceof Doctrine_Record && $this->_table->getTypeOf($k) != 'object') { unset($vars['_data'][$k]); } elseif ($v === self::$_null) { unset($vars['_data'][$k]); @@ -552,6 +552,23 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count $this->$k = $v; } + foreach ($this->_data as $k => $v) { + + switch ($this->_table->getTypeOf($k)) { + case 'array': + case 'object': + $this->_data[$k] = unserialize($this->_data[$k]); + break; + case 'gzip': + $this->_data[$k] = gzuncompress($this->_data[$k]); + break; + case 'enum': + $this->_data[$k] = $this->_table->enumValue($k, $this->_data[$k]); + break; + + } + } + $this->_table->getRepository()->add($this); $this->_filter = new Doctrine_Record_Filter($this); @@ -800,9 +817,11 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count $lower = strtolower($name); $lower = $this->_table->getColumnName($lower); + + $type = $this->_table->getTypeOf($name); if (isset($this->_data[$lower])) { - if ($value instanceof Doctrine_Record) { + if ($value instanceof Doctrine_Record && $type != 'object') { $id = $value->getIncremented(); if ($id !== null) { diff --git a/tests/Record/SerializeUnserializeTestCase.php b/tests/Record/SerializeUnserializeTestCase.php new file mode 100644 index 000000000..5581c56f0 --- /dev/null +++ b/tests/Record/SerializeUnserializeTestCase.php @@ -0,0 +1,154 @@ +. + */ + +/** + * Doctrine_Record_SerializeUnserialize_TestCase + * + * @package Doctrine + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Record_SerializeUnserialize_TestCase extends Doctrine_UnitTestCase +{ + + public function prepareTables() + { + $this->tables[] = 'SerializeTest'; + + parent::prepareTables(); + } + + public function prepareData() + { } + + public function testSerializeUnserialize() + { + $object = new SerializeTest(); + $object->booltest = true; + $object->integertest = 13; + $object->floattest = 0.13; + $object->stringtest = 'string'; + $object->arraytest = array(1, 2, 3); + $object->objecttest = new TestObject(13); + $object->enumtest = 'java'; + $object->blobtest = 'blobtest'; + $object->clobtest = 'clobtest'; + $object->gziptest = 'gziptest'; + $object->timestamptest = '2007-08-07 11:55:00'; + $object->timetest = '11:55:00'; + $object->datetest = '2007-08-07'; + + $object->save(); + + $object_before = clone($object); + $serialized = serialize($object); + $object_after = unserialize($serialized); + + $this->assertIdentical($object_before->booltest, $object_after->booltest); + $this->assertIdentical($object_before->integertest, $object_after->integertest); + $this->assertIdentical($object_before->floattest, $object_after->floattest); + $this->assertIdentical($object_before->stringtest, $object_after->stringtest); + $this->assertIdentical($object_before->arraytest, $object_after->arraytest); + $this->assertIdentical($object_before->enumtest, $object_after->enumtest); + $this->assertEqual($object_before->objecttest, $object_after->objecttest); + $this->assertIdentical($object_before->blobtest, $object_after->blobtest); + $this->assertIdentical($object_before->clobtest, $object_after->clobtest); + $this->assertIdentical($object_before->gziptest, $object_after->gziptest); + $this->assertIdentical($object_before->timestamptest, $object_after->timestamptest); + $this->assertIdentical($object_before->timetest, $object_after->timetest); + $this->assertIdentical($object_before->datetest, $object_after->datetest); + + } + + public function testSerializeUnserializeRecord() + { + $test = new TestRecord(); + $test->save(); + + $object = new SerializeTest(); + $object->objecttest = $test; + + $object->save(); + + $object_before = clone($object); + + $serialized = serialize($object); + $object_after = unserialize($serialized); + + $this->assertIdentical(get_class($object_after->objecttest), 'TestRecord'); + } + +} + +class SerializeTest extends Doctrine_Record +{ + public function setTableDefinition() + { + $this->setTableName('serialize_test'); + + $this->hasColumn('booltest', 'boolean'); + $this->hasColumn('integertest', 'integer', 4, array('unsigned' => true)); + $this->hasColumn('floattest', 'float'); + $this->hasColumn('stringtest', 'string', 200, array('fixed' => true)); + $this->hasColumn('arraytest', 'array', 10000); + $this->hasColumn('objecttest', 'object'); + $this->hasColumn('blobtest', 'blob'); + $this->hasColumn('clobtest', 'clob'); + $this->hasColumn('timestamptest', 'timestamp'); + $this->hasColumn('timetest', 'time'); + $this->hasColumn('datetest', 'date'); + $this->hasColumn('enumtest', 'enum', 4, + array( + 'values' => array( + 'php', + 'java', + 'python' + ) + ) + ); + $this->hasColumn('gziptest', 'gzip'); + } + +} + +class TestObject +{ + + private $test_field; + + public function __construct($value) + { + $this->test_field = $value; + } + +} + +class TestRecord extends Doctrine_Record +{ + public function setTableDefinition() + { + $this->setTableName('test'); + } +} diff --git a/tests/run.php b/tests/run.php index ec70639ff..f337df910 100644 --- a/tests/run.php +++ b/tests/run.php @@ -287,6 +287,7 @@ $test->addTestCase(new Doctrine_NewCore_TestCase()); // Record $test->addTestCase(new Doctrine_Record_State_TestCase()); +$test->addTestCase(new Doctrine_Record_SerializeUnserialize_TestCase()); // This test used to segfault php because of infinite recursion in Connection/UnitOfWork $test->addTestCase(new Doctrine_Record_Lock_TestCase());