Refactored Doctrine_Record, added Doctrine_Relation::isOneToOne
This commit is contained in:
parent
25956bea87
commit
88ef777fbd
@ -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
|
|||||||
|
|
||||||
$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
|
|||||||
*
|
*
|
||||||
* @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
|
|||||||
$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 ||
|
if($fk instanceof Doctrine_ForeignKey) {
|
||||||
$type == Doctrine_Relation::ONE_AGGREGATE) {
|
$this->references[$name]->set($fk->getForeign(),$this);
|
||||||
|
|
||||||
// ONE-TO-ONE
|
|
||||||
$this->references[$name] = $table->create();
|
|
||||||
|
|
||||||
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);
|
||||||
|
}
|
||||||
|
$this->originals[$name] = new Doctrine_Collection($table);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if($fk->isOneToOne()) {
|
||||||
|
// ONE-TO-ONE
|
||||||
|
$id = $this->get($local);
|
||||||
|
|
||||||
switch($fk->getType()):
|
if($fk instanceof Doctrine_LocalKey) {
|
||||||
case Doctrine_Relation::ONE_COMPOSITE:
|
|
||||||
case Doctrine_Relation::ONE_AGGREGATE:
|
|
||||||
|
|
||||||
// ONE-TO-ONE
|
if(empty($id)) {
|
||||||
$id = $this->get($local);
|
$this->references[$name] = $table->create();
|
||||||
|
$this->set($fk->getLocal(),$this->references[$name]);
|
||||||
|
} else {
|
||||||
|
|
||||||
if($fk instanceof Doctrine_LocalKey) {
|
$record = $table->find($id);
|
||||||
|
|
||||||
if(empty($id)) {
|
if($record !== false)
|
||||||
$this->references[$name] = $table->create();
|
$this->references[$name] = $record;
|
||||||
$this->set($fk->getLocal(),$this->references[$name]);
|
else
|
||||||
} else {
|
$this->references[$name] = $table->create();
|
||||||
|
|
||||||
$record = $table->find($id);
|
|
||||||
|
|
||||||
if($record !== false)
|
|
||||||
$this->references[$name] = $record;
|
|
||||||
else
|
|
||||||
$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);
|
|
||||||
|
|
||||||
// ONE-TO-MANY
|
$query = $fk->getRelationDql(1);
|
||||||
if($fk instanceof Doctrine_ForeignKey) {
|
|
||||||
$id = $this->get($local);
|
// ONE-TO-MANY
|
||||||
$coll = $graph->query($query,array($id));
|
if($fk instanceof Doctrine_ForeignKey) {
|
||||||
$coll->setReference($this, $fk);
|
$id = $this->get($local);
|
||||||
} elseif($fk instanceof Doctrine_Association_Self) {
|
$coll = $graph->query($query,array($id));
|
||||||
$coll = $fk->fetchRelatedFor($this);
|
$coll->setReference($this, $fk);
|
||||||
} elseif($fk instanceof Doctrine_Association) {
|
} elseif($fk instanceof Doctrine_Association_Self) {
|
||||||
$id = $this->getIncremented();
|
$coll = $fk->fetchRelatedFor($this);
|
||||||
$coll = $graph->query($query, array($id));
|
} elseif($fk instanceof Doctrine_Association) {
|
||||||
}
|
$id = $this->getIncremented();
|
||||||
$this->references[$name] = $coll;
|
$coll = $graph->query($query, array($id));
|
||||||
$this->originals[$name] = clone $coll;
|
}
|
||||||
endswitch;
|
$this->references[$name] = $coll;
|
||||||
break;
|
$this->originals[$name] = clone $coll;
|
||||||
endswitch;
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* filterRelated
|
* filterRelated
|
||||||
|
@ -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
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user