Commit 88ef777f authored by zYne's avatar zYne

Refactored Doctrine_Record, added Doctrine_Relation::isOneToOne

parent 25956bea
...@@ -349,19 +349,14 @@ abstract class Doctrine_Hydrate extends Doctrine_Access { ...@@ -349,19 +349,14 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
$last = $prev[$pointer]->getLast(); $last = $prev[$pointer]->getLast();
switch($fk->getType()): if( ! $fk->isOneToOne()) {
case Doctrine_Relation::ONE_COMPOSITE: if($last instanceof Doctrine_Record) {
case Doctrine_Relation::ONE_AGGREGATE: if( ! $last->hasReference($alias)) {
$prev[$name] = $this->getCollection($name);
break; $last->initReference($prev[$name],$fk);
default:
if($last instanceof Doctrine_Record) {
if( ! $last->hasReference($alias)) {
$prev[$name] = $this->getCollection($name);
$last->initReference($prev[$name],$fk);
}
} }
endswitch; }
}
continue; continue;
} }
......
...@@ -453,13 +453,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -453,13 +453,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->cleanData(); $this->cleanData();
$exists = true; $this->prepareIdentifiers($this->exists());
if($this->state == Doctrine_Record::STATE_TDIRTY ||
$this->state == Doctrine_Record::STATE_TCLEAN)
$exists = false;
$this->prepareIdentifiers($exists);
$this->table->getAttribute(Doctrine::ATTR_LISTENER)->onWakeUp($this); $this->table->getAttribute(Doctrine::ATTR_LISTENER)->onWakeUp($this);
} }
...@@ -1158,16 +1152,24 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1158,16 +1152,24 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* *
* @param Doctrine_Collection $coll * @param Doctrine_Collection $coll
* @param Doctrine_Relation $connector * @param Doctrine_Relation $connector
* @return void * @return boolean
*/ */
public function initReference(Doctrine_Collection $coll, Doctrine_Relation $connector) { public function initReference(Doctrine_Collection $coll, Doctrine_Relation $connector) {
$alias = $connector->getAlias(); $alias = $connector->getAlias();
if( ! ($connector instanceof Doctrine_Association)) if(isset($this->references[$alias]))
$coll->setReference($this, $connector); return false;
$this->references[$alias] = $coll; if( ! $connector->isOneToOne()) {
$this->originals[$alias] = clone $coll; if( ! ($connector instanceof Doctrine_Association))
$coll->setReference($this, $connector);
$this->references[$alias] = $coll;
$this->originals[$alias] = clone $coll;
return true;
}
return false;
} }
/** /**
* addReference * addReference
...@@ -1216,91 +1218,78 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1216,91 +1218,78 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$graph = $table->getQueryObject(); $graph = $table->getQueryObject();
$type = $fk->getType(); $type = $fk->getType();
switch($this->getState()): if( ! $this->exists()) {
case Doctrine_Record::STATE_TDIRTY: if($fk->isOneToOne()) {
case Doctrine_Record::STATE_TCLEAN: // ONE-TO-ONE
$this->references[$name] = $table->create();
if($type == Doctrine_Relation::ONE_COMPOSITE ||
$type == Doctrine_Relation::ONE_AGGREGATE) {
// ONE-TO-ONE if($fk instanceof Doctrine_ForeignKey) {
$this->references[$name] = $table->create(); $this->references[$name]->set($fk->getForeign(),$this);
if($fk instanceof Doctrine_ForeignKey) {
$this->references[$name]->set($fk->getForeign(),$this);
} else {
$this->set($fk->getLocal(),$this->references[$name]);
}
} else { } else {
$this->references[$name] = new Doctrine_Collection($table); $this->set($fk->getLocal(),$this->references[$name]);
if($fk instanceof Doctrine_ForeignKey) {
// ONE-TO-MANY
$this->references[$name]->setReference($this,$fk);
}
$this->originals[$name] = new Doctrine_Collection($table);
} }
break; } else {
case Doctrine_Record::STATE_DIRTY: $this->references[$name] = new Doctrine_Collection($table);
case Doctrine_Record::STATE_CLEAN: if($fk instanceof Doctrine_ForeignKey) {
case Doctrine_Record::STATE_PROXY: // ONE-TO-MANY
$this->references[$name]->setReference($this,$fk);
switch($fk->getType()): }
case Doctrine_Relation::ONE_COMPOSITE: $this->originals[$name] = new Doctrine_Collection($table);
case Doctrine_Relation::ONE_AGGREGATE: }
} else {
// ONE-TO-ONE if($fk->isOneToOne()) {
$id = $this->get($local); // ONE-TO-ONE
$id = $this->get($local);
if($fk instanceof Doctrine_LocalKey) { if($fk instanceof Doctrine_LocalKey) {
if(empty($id)) { if(empty($id)) {
$this->references[$name] = $table->create(); $this->references[$name] = $table->create();
$this->set($fk->getLocal(),$this->references[$name]); $this->set($fk->getLocal(),$this->references[$name]);
} else { } else {
$record = $table->find($id); $record = $table->find($id);
if($record !== false) if($record !== false)
$this->references[$name] = $record; $this->references[$name] = $record;
else else
$this->references[$name] = $table->create(); $this->references[$name] = $table->create();
//$this->set($fk->getLocal(),$this->references[$name]); //$this->set($fk->getLocal(),$this->references[$name]);
} }
} elseif ($fk instanceof Doctrine_ForeignKey) { } elseif ($fk instanceof Doctrine_ForeignKey) {
if(empty($id)) { if(empty($id)) {
$this->references[$name] = $table->create(); $this->references[$name] = $table->create();
$this->references[$name]->set($fk->getForeign(), $this); $this->references[$name]->set($fk->getForeign(), $this);
} else { } else {
$dql = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".".$fk->getForeign()." = ?"; $dql = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".".$fk->getForeign()." = ?";
$coll = $graph->query($dql, array($id)); $coll = $graph->query($dql, array($id));
$this->references[$name] = $coll[0]; $this->references[$name] = $coll[0];
$this->references[$name]->set($fk->getForeign(), $this); $this->references[$name]->set($fk->getForeign(), $this);
} }
} }
break; } else {
default:
$query = $fk->getRelationDql(1); $query = $fk->getRelationDql(1);
// ONE-TO-MANY // ONE-TO-MANY
if($fk instanceof Doctrine_ForeignKey) { if($fk instanceof Doctrine_ForeignKey) {
$id = $this->get($local); $id = $this->get($local);
$coll = $graph->query($query,array($id)); $coll = $graph->query($query,array($id));
$coll->setReference($this, $fk); $coll->setReference($this, $fk);
} elseif($fk instanceof Doctrine_Association_Self) { } elseif($fk instanceof Doctrine_Association_Self) {
$coll = $fk->fetchRelatedFor($this); $coll = $fk->fetchRelatedFor($this);
} elseif($fk instanceof Doctrine_Association) { } elseif($fk instanceof Doctrine_Association) {
$id = $this->getIncremented(); $id = $this->getIncremented();
$coll = $graph->query($query, array($id)); $coll = $graph->query($query, array($id));
} }
$this->references[$name] = $coll; $this->references[$name] = $coll;
$this->originals[$name] = clone $coll; $this->originals[$name] = clone $coll;
endswitch; }
break; }
endswitch;
} }
/** /**
* filterRelated * filterRelated
......
...@@ -131,6 +131,16 @@ class Doctrine_Relation { ...@@ -131,6 +131,16 @@ class Doctrine_Relation {
final public function getForeign() { final public function getForeign() {
return $this->foreign; return $this->foreign;
} }
/**
* isOneToOne
* returns whether or not this relation is a one-to-one relation
*
* @return boolean
*/
final public function isOneToOne() {
return ($this->type == Doctrine_Relation::ONE_AGGREGATE ||
$this->type == Doctrine_Relation::ONE_COMPOSITE);
}
/** /**
* getRelationDql * getRelationDql
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment