1
0
mirror of synced 2024-12-13 22:56:04 +03:00

Fixes #154, has* relations should act as owns* when saving related objects

This commit is contained in:
zYne 2006-10-08 18:43:13 +00:00
parent 11206fe573
commit 966450d454
2 changed files with 40 additions and 52 deletions

View File

@ -521,66 +521,57 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$alias = $this->table->getAlias($name);
if($rel instanceof Doctrine_Relation_Association) {
switch($rel->getType()):
case Doctrine_Relation::MANY_COMPOSITE:
break;
case Doctrine_Relation::MANY_AGGREGATE:
$asf = $rel->getAssociationFactory();
if($record->hasReference($alias)) {
$asf = $rel->getAssociationFactory();
$new = $record->getReference($alias);
if($record->hasReference($alias)) {
if( ! $this->hasOriginalsFor($alias)) {
$record->loadReference($alias);
}
$new = $record->getReference($alias);
$operations = Doctrine_Relation::getDeleteOperations($this->originals[$alias],$new);
if( ! $this->hasOriginalsFor($alias))
$record->loadReference($alias);
foreach($operations as $r) {
$query = "DELETE FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." = ?"
$operations = Doctrine_Relation::getDeleteOperations($this->originals[$alias],$new);
foreach($operations as $r) {
$query = "DELETE FROM ".$asf->getTableName()." WHERE ".$fk->getForeign()." = ?"
." AND ".$fk->getLocal()." = ?";
$this->table->getConnection()->execute($query, array($r->getIncremented(),$record->getIncremented()));
}
$this->table->getConnection()->execute($query, array($r->getIncremented(),$record->getIncremented()));
}
$operations = Doctrine_Relation::getInsertOperations($this->originals[$alias],$new);
foreach($operations as $r) {
$reldao = $asf->create();
$reldao->set($fk->getForeign(),$r);
$reldao->set($fk->getLocal(),$this);
$reldao->save();
$operations = Doctrine_Relation::getInsertOperations($this->originals[$alias],$new);
foreach($operations as $r) {
$reldao = $asf->create();
$reldao->set($fk->getForeign(),$r);
$reldao->set($fk->getLocal(),$this);
$reldao->save();
}
$this->originals[$alias] = clone $this->references[$alias];
}
break;
endswitch;
}
$this->originals[$alias] = clone $this->references[$alias];
}
} elseif($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_Relation_LocalKey) {
switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE:
if(isset($this->originals[$alias]) && $this->originals[$alias]->obtainIdentifier() != $this->references[$alias]->obtainIdentifier())
if($fk->isOneToOne()) {
if(isset($this->originals[$alias]) && $this->originals[$alias]->obtainIdentifier() != $this->references[$alias]->obtainIdentifier())
$this->originals[$alias]->delete();
} else {
if(isset($this->references[$alias])) {
$new = $this->references[$alias];
break;
case Doctrine_Relation::MANY_COMPOSITE:
if(isset($this->references[$alias])) {
$new = $this->references[$alias];
if( ! isset($this->originals[$alias]))
$record->loadReference($alias);
if( ! isset($this->originals[$alias]))
$record->loadReference($alias);
$operations = Doctrine_Relation::getDeleteOperations($this->originals[$alias], $new);
$operations = Doctrine_Relation::getDeleteOperations($this->originals[$alias], $new);
foreach($operations as $r) {
$r->delete();
}
$record->assignOriginals($alias, clone $this->references[$alias]);
foreach($operations as $r) {
$r->delete();
}
break;
endswitch;
$record->assignOriginals($alias, clone $this->references[$alias]);
}
}
}
endforeach;
}

View File

@ -994,7 +994,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @return void
*/
final public function saveAssociations() {
foreach($this->table->getRelations() as $fk):
foreach($this->table->getRelations() as $fk) {
$table = $fk->getTable();
$name = $table->getComponentName();
$alias = $this->table->getAlias($name);
@ -1035,13 +1035,11 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} elseif($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_Relation_LocalKey) {
switch($fk->getType()):
case Doctrine_Relation::ONE_COMPOSITE:
if($fk->isOneToOne()) {
if(isset($this->originals[$alias]) && $this->originals[$alias]->obtainIdentifier() != $this->references[$alias]->obtainIdentifier())
$this->originals[$alias]->delete();
break;
case Doctrine_Relation::MANY_COMPOSITE:
} else {
if(isset($this->references[$alias])) {
$new = $this->references[$alias];
@ -1056,10 +1054,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->originals[$alias] = clone $this->references[$alias];
}
break;
endswitch;
}
}
endforeach;
}
}
/**
* getOriginals