1
0
mirror of synced 2024-12-14 07:06: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); $alias = $this->table->getAlias($name);
if($rel instanceof Doctrine_Relation_Association) { 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)) { $new = $record->getReference($alias);
$record->loadReference($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()." = ?"; ." 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); $operations = Doctrine_Relation::getInsertOperations($this->originals[$alias],$new);
foreach($operations as $r) { foreach($operations as $r) {
$reldao = $asf->create(); $reldao = $asf->create();
$reldao->set($fk->getForeign(),$r); $reldao->set($fk->getForeign(),$r);
$reldao->set($fk->getLocal(),$this); $reldao->set($fk->getLocal(),$this);
$reldao->save(); $reldao->save();
} }
$this->originals[$alias] = clone $this->references[$alias]; $this->originals[$alias] = clone $this->references[$alias];
} }
break;
endswitch;
} elseif($fk instanceof Doctrine_Relation_ForeignKey || } elseif($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_Relation_LocalKey) { $fk instanceof Doctrine_Relation_LocalKey) {
switch($fk->getType()): if($fk->isOneToOne()) {
case Doctrine_Relation::ONE_COMPOSITE: if(isset($this->originals[$alias]) && $this->originals[$alias]->obtainIdentifier() != $this->references[$alias]->obtainIdentifier())
if(isset($this->originals[$alias]) && $this->originals[$alias]->obtainIdentifier() != $this->references[$alias]->obtainIdentifier())
$this->originals[$alias]->delete(); $this->originals[$alias]->delete();
} else {
if(isset($this->references[$alias])) {
$new = $this->references[$alias];
break; if( ! isset($this->originals[$alias]))
case Doctrine_Relation::MANY_COMPOSITE: $record->loadReference($alias);
if(isset($this->references[$alias])) {
$new = $this->references[$alias];
if( ! isset($this->originals[$alias])) $operations = Doctrine_Relation::getDeleteOperations($this->originals[$alias], $new);
$record->loadReference($alias);
$operations = Doctrine_Relation::getDeleteOperations($this->originals[$alias], $new); foreach($operations as $r) {
$r->delete();
foreach($operations as $r) {
$r->delete();
}
$record->assignOriginals($alias, clone $this->references[$alias]);
} }
break;
endswitch; $record->assignOriginals($alias, clone $this->references[$alias]);
}
}
} }
endforeach; endforeach;
} }

View File

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