1
0
mirror of synced 2025-02-06 23:39:25 +03:00

Serialization model rewrite

This commit is contained in:
doctrine 2006-06-05 09:57:53 +00:00
parent 02a283f5e9
commit bcf9ca78e0
5 changed files with 93 additions and 40 deletions

View File

@ -8,7 +8,7 @@ require_once("Access.php");
* @url www.phpdoctrine.com * @url www.phpdoctrine.com
* @license LGPL * @license LGPL
*/ */
class Doctrine_Collection extends Doctrine_Access implements Countable, IteratorAggregate { class Doctrine_Collection extends Doctrine_Access implements Countable, IteratorAggregate, Serializable {
/** /**
* @var array $data an array containing the data access objects of this collection * @var array $data an array containing the data access objects of this collection
*/ */
@ -69,6 +69,51 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
public function getTable() { public function getTable() {
return $this->table; return $this->table;
} }
/**
* this method is automatically called when this Doctrine_Collection is serialized
*
* @return array
*/
public function serialize() {
$vars = get_object_vars($this);
unset($vars['reference']);
unset($vars['reference_field']);
unset($vars['relation']);
unset($vars['expandable']);
unset($vars['expanded']);
unset($vars['generator']);
$vars['table'] = $vars['table']->getComponentName();
return serialize($vars);
}
/**
* unseralize
* this method is automatically called everytime a Doctrine_Collection object is unserialized
*
* @return void
*/
public function unserialize($serialized) {
$manager = Doctrine_Manager::getInstance();
$session = $manager->getCurrentSession();
$array = unserialize($serialized);
foreach($array as $name => $values) {
$this->$name = $values;
}
$this->table = $session->getTable($this->table->getComponenName());
$this->expanded = array();
$this->expandable = true;
$name = $table->getAttribute(Doctrine::ATTR_COLL_KEY);
if($name !== null) {
$this->generator = new Doctrine_IndexGenerator($name);
}
}
/** /**
* whether or not an offset batch has been expanded * whether or not an offset batch has been expanded
* @return boolean * @return boolean

View File

@ -3,7 +3,7 @@ require_once("Access.php");
/** /**
* Doctrine_Record * Doctrine_Record
*/ */
abstract class Doctrine_Record extends Doctrine_Access implements Countable, IteratorAggregate { abstract class Doctrine_Record extends Doctrine_Access implements Countable, IteratorAggregate, Serializable {
/** /**
* STATE CONSTANTS * STATE CONSTANTS
*/ */
@ -247,7 +247,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$name = $this->table->getIdentifier(); $name = $this->table->getIdentifier();
if($exists) { if($exists) {
if(isset($this->data[$name])) if(isset($this->data[$name]) && $this->data[$name] !== self::$null)
$this->id = $this->data[$name]; $this->id = $this->data[$name];
} }
@ -272,21 +272,24 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* *
* @return array * @return array
*/ */
public function __sleep() { public function serialize() {
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onSleep($this); $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onSleep($this);
$vars = get_object_vars($this);
// unset all vars that won't need to be serialized unset($vars['references']);
unset($vars['collections']);
unset($vars['originals']);
unset($vars['table']);
unset($this->associations); if( ! is_array($this->id)) {
unset($this->collections); $name = $this->table->getIdentifier();
unset($this->references); $this->data = array_merge($this->data, array($name => $this->id));
unset($this->originals); }
unset($this->oid);
foreach($this->data as $k => $v) { foreach($this->data as $k => $v) {
if($v instanceof Doctrine_Record) if($v instanceof Doctrine_Record)
$this->data[$k] = array(); unset($this->data[$k]);
elseif($v === self::$null) { elseif($v === self::$null) {
unset($this->data[$k]); unset($this->data[$k]);
} else { } else {
@ -299,10 +302,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
} }
$this->table = $this->table->getComponentName(); return serialize($vars);
return array_keys(get_object_vars($this));
} }
/** /**
* unseralize * unseralize
@ -310,17 +310,21 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* *
* @return void * @return void
*/ */
public function __wakeup() { public function unserialize($serialized) {
$name = $this->table;
$manager = Doctrine_Manager::getInstance(); $manager = Doctrine_Manager::getInstance();
$sess = $manager->getCurrentSession(); $session = $manager->getCurrentSession();
$this->oid = self::$index; $this->oid = self::$index;
self::$index++; self::$index++;
$this->table = $sess->getTable($name); $this->table = $session->getTable(get_class($this));
$array = unserialize($serialized);
foreach($array as $name => $values) {
$this->$name = $values;
}
$this->table->getRepository()->add($this); $this->table->getRepository()->add($this);
@ -336,6 +340,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onWakeUp($this); $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onWakeUp($this);
} }
/** /**
* addCollection * addCollection
* @param Doctrine_Collection $collection * @param Doctrine_Collection $collection
@ -494,9 +500,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
return $this->data[$name]; return $this->data[$name];
} }
if($name == $this->table->getIdentifier()) { if($name === $this->table->getIdentifier())
return $this->id; return $this->id;
}
if( ! isset($this->references[$name])) if( ! isset($this->references[$name]))
$this->loadReference($name); $this->loadReference($name);
@ -905,6 +911,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->state = Doctrine_Record::STATE_CLEAN; $this->state = Doctrine_Record::STATE_CLEAN;
$this->modified = array(); $this->modified = array();
} else { } else {
$name = $this->table->getIdentifier();
$this->id = $id; $this->id = $id;
$this->state = Doctrine_Record::STATE_CLEAN; $this->state = Doctrine_Record::STATE_CLEAN;
$this->modified = array(); $this->modified = array();

View File

@ -78,11 +78,10 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
*/ */
public function __construct(Doctrine_Manager $manager,PDO $pdo) { public function __construct(Doctrine_Manager $manager,PDO $pdo) {
$this->dbh = $pdo; $this->dbh = $pdo;
$this->state = Doctrine_Session::STATE_OPEN;
$this->setParent($manager); $this->setParent($manager);
$this->state = Doctrine_Session::STATE_OPEN;
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER); $this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

View File

@ -14,7 +14,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$this->dbh->query("DROP TABLE IF EXISTS test_entries"); $this->dbh->query("DROP TABLE IF EXISTS test_entries");
parent::prepareTables(); parent::prepareTables();
} }
public function testOneToOneRelationFetching2() { public function testOneToOneSharedRelations() {
$status = new Log_Status(); $status = new Log_Status();
$status->name = 'success'; $status->name = 'success';

View File

@ -3,10 +3,12 @@ require_once("UnitTestCase.class.php");
class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
public function testSerialize() { public function testSerialize() {
//$user = $this->session->getTable("User")->find(4); $user = $this->session->getTable("User")->find(4);
//$str = serialize($user); $str = serialize($user);
//$user2 = unserialize($str); $user2 = unserialize($str);
//$this->assertEqual($user2->getID(),$user->getID());
$this->assertTrue($user2 instanceof User);
$this->assertEqual($user2->getID(),$user->getID());
} }
public function testCallback() { public function testCallback() {