Serialization model rewrite
This commit is contained in:
parent
02a283f5e9
commit
bcf9ca78e0
@ -8,7 +8,7 @@ require_once("Access.php");
|
||||
* @url www.phpdoctrine.com
|
||||
* @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
|
||||
*/
|
||||
@ -69,6 +69,51 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
|
||||
public function getTable() {
|
||||
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
|
||||
* @return boolean
|
||||
|
@ -3,7 +3,7 @@ require_once("Access.php");
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@ -247,10 +247,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
$name = $this->table->getIdentifier();
|
||||
|
||||
if($exists) {
|
||||
if(isset($this->data[$name]))
|
||||
if(isset($this->data[$name]) && $this->data[$name] !== self::$null)
|
||||
$this->id = $this->data[$name];
|
||||
}
|
||||
|
||||
|
||||
unset($this->data[$name]);
|
||||
|
||||
break;
|
||||
@ -272,21 +272,24 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function __sleep() {
|
||||
public function serialize() {
|
||||
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onSleep($this);
|
||||
|
||||
$vars = get_object_vars($this);
|
||||
|
||||
unset($vars['references']);
|
||||
unset($vars['collections']);
|
||||
unset($vars['originals']);
|
||||
unset($vars['table']);
|
||||
|
||||
// unset all vars that won't need to be serialized
|
||||
|
||||
unset($this->associations);
|
||||
unset($this->collections);
|
||||
unset($this->references);
|
||||
unset($this->originals);
|
||||
unset($this->oid);
|
||||
if( ! is_array($this->id)) {
|
||||
$name = $this->table->getIdentifier();
|
||||
$this->data = array_merge($this->data, array($name => $this->id));
|
||||
}
|
||||
|
||||
foreach($this->data as $k => $v) {
|
||||
if($v instanceof Doctrine_Record)
|
||||
$this->data[$k] = array();
|
||||
unset($this->data[$k]);
|
||||
elseif($v === self::$null) {
|
||||
unset($this->data[$k]);
|
||||
} else {
|
||||
@ -298,11 +301,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
endswitch;
|
||||
}
|
||||
}
|
||||
|
||||
$this->table = $this->table->getComponentName();
|
||||
|
||||
return array_keys(get_object_vars($this));
|
||||
|
||||
return serialize($vars);
|
||||
}
|
||||
/**
|
||||
* unseralize
|
||||
@ -310,17 +310,21 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __wakeup() {
|
||||
$name = $this->table;
|
||||
|
||||
public function unserialize($serialized) {
|
||||
$manager = Doctrine_Manager::getInstance();
|
||||
$sess = $manager->getCurrentSession();
|
||||
$session = $manager->getCurrentSession();
|
||||
|
||||
$this->oid = 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);
|
||||
|
||||
@ -336,6 +340,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
|
||||
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onWakeUp($this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* addCollection
|
||||
* @param Doctrine_Collection $collection
|
||||
@ -494,9 +500,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
return $this->data[$name];
|
||||
}
|
||||
|
||||
if($name == $this->table->getIdentifier()) {
|
||||
if($name === $this->table->getIdentifier())
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
|
||||
if( ! isset($this->references[$name]))
|
||||
$this->loadReference($name);
|
||||
@ -579,7 +585,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
$fk = $this->table->getForeignKey($name);
|
||||
|
||||
// one-to-many or one-to-one relation
|
||||
if($fk instanceof Doctrine_ForeignKey ||
|
||||
if($fk instanceof Doctrine_ForeignKey ||
|
||||
$fk instanceof Doctrine_LocalKey) {
|
||||
switch($fk->getType()):
|
||||
case Doctrine_Relation::MANY_COMPOSITE:
|
||||
@ -702,7 +708,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
$type = $this->table->getTypeOf($v);
|
||||
|
||||
if($type == 'array' ||
|
||||
$type == 'object') {
|
||||
$type == 'object') {
|
||||
|
||||
$a[$v] = serialize($this->data[$v]);
|
||||
continue;
|
||||
@ -905,9 +911,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
|
||||
$this->state = Doctrine_Record::STATE_CLEAN;
|
||||
$this->modified = array();
|
||||
} else {
|
||||
$this->id = $id;
|
||||
$this->state = Doctrine_Record::STATE_CLEAN;
|
||||
$this->modified = array();
|
||||
$name = $this->table->getIdentifier();
|
||||
$this->id = $id;
|
||||
$this->state = Doctrine_Record::STATE_CLEAN;
|
||||
$this->modified = array();
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
@ -77,12 +77,11 @@ abstract class Doctrine_Session extends Doctrine_Configurable implements Countab
|
||||
* @param PDO $pdo -- database handle
|
||||
*/
|
||||
public function __construct(Doctrine_Manager $manager,PDO $pdo) {
|
||||
$this->dbh = $pdo;
|
||||
|
||||
$this->setParent($manager);
|
||||
|
||||
$this->dbh = $pdo;
|
||||
$this->state = Doctrine_Session::STATE_OPEN;
|
||||
|
||||
$this->setParent($manager);
|
||||
|
||||
$this->dbh->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
|
||||
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
|
@ -14,7 +14,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
|
||||
$this->dbh->query("DROP TABLE IF EXISTS test_entries");
|
||||
parent::prepareTables();
|
||||
}
|
||||
public function testOneToOneRelationFetching2() {
|
||||
public function testOneToOneSharedRelations() {
|
||||
$status = new Log_Status();
|
||||
$status->name = 'success';
|
||||
|
||||
@ -45,7 +45,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
|
||||
$this->assertEqual($entries[0]->Log_Status->name, 'success');
|
||||
|
||||
// the second Log_Status is fetched from identityMap
|
||||
|
||||
|
||||
$this->assertTrue($entries[1]->Log_Status instanceof Log_Status);
|
||||
$this->assertEqual($entries[1]->Log_Status->name, 'success');
|
||||
|
||||
|
@ -3,10 +3,12 @@ require_once("UnitTestCase.class.php");
|
||||
|
||||
class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
|
||||
public function testSerialize() {
|
||||
//$user = $this->session->getTable("User")->find(4);
|
||||
//$str = serialize($user);
|
||||
//$user2 = unserialize($str);
|
||||
//$this->assertEqual($user2->getID(),$user->getID());
|
||||
$user = $this->session->getTable("User")->find(4);
|
||||
$str = serialize($user);
|
||||
$user2 = unserialize($str);
|
||||
|
||||
$this->assertTrue($user2 instanceof User);
|
||||
$this->assertEqual($user2->getID(),$user->getID());
|
||||
}
|
||||
|
||||
public function testCallback() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user