Commit 599dc759 authored by zYne's avatar zYne

Fixes #180

parent 20f50425
...@@ -540,7 +540,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -540,7 +540,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$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($record->obtainOriginals($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);
...@@ -548,14 +548,14 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -548,14 +548,14 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
$reldao->save(); $reldao->save();
} }
$this->originals[$alias] = clone $this->references[$alias]; $record->assignOriginals($alias, clone $this->references[$alias]);
} }
} elseif($fk instanceof Doctrine_Relation_ForeignKey || } elseif($fk instanceof Doctrine_Relation_ForeignKey ||
$fk instanceof Doctrine_Relation_LocalKey) { $fk instanceof Doctrine_Relation_LocalKey) {
if($fk->isOneToOne()) { if($fk->isOneToOne()) {
if(isset($this->originals[$alias]) && $this->originals[$alias]->obtainIdentifier() != $this->references[$alias]->obtainIdentifier()) if($record->obtainOriginals($alias) && $record->obtainOriginals($alias)->obtainIdentifier() != $this->references[$alias]->obtainIdentifier())
$this->originals[$alias]->delete(); $record->obtainOriginals($alias)->delete();
} else { } else {
if(isset($this->references[$alias])) { if(isset($this->references[$alias])) {
$new = $this->references[$alias]; $new = $this->references[$alias];
......
...@@ -434,7 +434,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -434,7 +434,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$vars = get_object_vars($this); $vars = get_object_vars($this);
unset($vars['references']); unset($vars['references']);
unset($vars['collections']);
unset($vars['originals']); unset($vars['originals']);
unset($vars['_table']); unset($vars['_table']);
...@@ -854,12 +853,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -854,12 +853,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
return $a; return $a;
} }
/** /**
* getPrepared
*
* returns an array of modified fields and values with data preparation * returns an array of modified fields and values with data preparation
* adds column aggregation inheritance and converts Records into primary key values * adds column aggregation inheritance and converts Records into primary key values
* *
* @param array $array
* @return array * @return array
*/ */
final public function getPrepared(array $array = array()) { public function getPrepared(array $array = array()) {
$a = array(); $a = array();
if(empty($array)) if(empty($array))
...@@ -968,6 +970,14 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -968,6 +970,14 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
public function getIterator() { public function getIterator() {
return new Doctrine_Record_Iterator($this); return new Doctrine_Record_Iterator($this);
} }
public function obtainOriginals($name) {
if(isset($this->originals[$name]))
return $this->originals[$name];
return false;
}
/** /**
* saveAssociations * saveAssociations
* *
......
...@@ -89,7 +89,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -89,7 +89,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
/** /**
* @var array $columns an array of column definitions * @var array $columns an array of column definitions
*/ */
private $columns; private $columns = array();
/** /**
* @var array $bound bound relations * @var array $bound bound relations
*/ */
...@@ -103,8 +103,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -103,8 +103,6 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
* determining its state * determining its state
*/ */
private $columnCount; private $columnCount;
/** /**
* @var array $inheritanceMap inheritanceMap is used for inheritance mapping, keys representing columns and values * @var array $inheritanceMap inheritanceMap is used for inheritance mapping, keys representing columns and values
* the column values that should correspond to child classes * the column values that should correspond to child classes
...@@ -229,7 +227,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -229,7 +227,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
} }
} else { } else {
throw new Doctrine_Exception("Class '$name' has no table definition."); throw new Doctrine_Table_Exception("Class '$name' has no table definition.");
} }
...@@ -613,7 +611,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -613,7 +611,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
throw new Doctrine_Table_Exception("Only one-to-one relations are possible when local reference key is used."); throw new Doctrine_Table_Exception("Only one-to-one relations are possible when local reference key is used.");
} elseif($component == $name || } elseif($component == $name ||
($component == $alias && ($name == $this->name || in_array($name,$this->parents)))) { ($component == $alias)) { // && ($name == $this->name || in_array($name,$this->parents))
if( ! isset($local)) if( ! isset($local))
$local = $this->identifier; $local = $this->identifier;
...@@ -666,9 +664,11 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { ...@@ -666,9 +664,11 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable {
} }
} }
$this->relations[$alias] = $relation; $this->relations[$alias] = $relation;
return $this->relations[$alias]; return $this->relations[$alias];
} }
// load all relations // load all relations
$this->getRelations(); $this->getRelations();
......
<?php <?php
class RelationTest extends Doctrine_Record {
public function setTableDefinition() {
}
public function setUp() {
$this->ownsMany('OwnsOneToManyWithAlias as AliasO2M', 'AliasO2M.component_id');
$this->hasMany('HasManyToManyWithAlias as AliasM2M', 'JoinTable.c1_id');
}
}
class HasOneToOne extends Doctrine_Record {
}
class HasOneToOneWithAlias extends Doctrine_Record {
}
class JoinTable extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn('c1_id', 'integer');
$this->hasColumn('c2_id', 'integer');
}
}
class HasManyWithAlias extends Doctrine_Record {
}
class OwnsOneToManyWithAlias extends Doctrine_Record {
public function setTableDefinition() {
$this->hasColumn('component_id', 'integer');
}
public function setUp() {
}
}
class HasManyToManyWithAlias extends Doctrine_Record {
public function setTableDefinition() { }
public function setUp() {
$this->hasMany('RelationTest as AliasM2M', 'JoinTable.c2_id');
}
}
class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase { class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase {
public function prepareData() { } public function prepareData() { }
public function prepareTables() { public function prepareTables() {
$this->tables = array(); $this->tables = array();
} }
public function testOneToManyOwnsRelationWithAliases() {
$this->manager->setAttribute(Doctrine::ATTR_CREATE_TABLES, false);
$component = new RelationTest();
try {
$rel = $component->getTable()->getRelation('AliasO2M');
$this->pass();
} catch(Doctrine_Exception $e) {
$this->fail();
}
$this->assertTrue($rel instanceof Doctrine_Relation_ForeignKey);
}
public function testManyToManyHasRelationWithAliases() {
$component = new RelationTest();
try {
$rel = $component->getTable()->getRelation('AliasM2M');
$this->pass();
} catch(Doctrine_Exception $e) {
$this->fail();
}
$this->assertTrue($rel instanceof Doctrine_Relation_Association);
}
public function testManyToManyRelation() { public function testManyToManyRelation() {
$user = new User(); $user = new User();
...@@ -31,5 +95,7 @@ class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase { ...@@ -31,5 +95,7 @@ class Doctrine_Relation_TestCase extends Doctrine_UnitTestCase {
$user = new User(); $user = new User();
$this->assertTrue($user->getTable()->getRelation('Phonenumber') instanceof Doctrine_Relation_ForeignKey); $this->assertTrue($user->getTable()->getRelation('Phonenumber') instanceof Doctrine_Relation_ForeignKey);
$this->manager->setAttribute(Doctrine::ATTR_CREATE_TABLES, true);
} }
} }
...@@ -56,14 +56,14 @@ print "<pre>"; ...@@ -56,14 +56,14 @@ print "<pre>";
$test = new GroupTest("Doctrine Framework Unit Tests"); $test = new GroupTest("Doctrine Framework Unit Tests");
$test->addTestCase(new Doctrine_Relation_TestCase());
$test->addTestCase(new Doctrine_RecordTestCase()); $test->addTestCase(new Doctrine_RecordTestCase());
$test->addTestCase(new Doctrine_ValidatorTestCase()); $test->addTestCase(new Doctrine_ValidatorTestCase());
$test->addTestCase(new Doctrine_Query_MultiJoin_TestCase()); $test->addTestCase(new Doctrine_Query_MultiJoin_TestCase());
$test->addTestCase(new Doctrine_Relation_TestCase());
$test->addTestCase(new Doctrine_EventListenerTestCase()); $test->addTestCase(new Doctrine_EventListenerTestCase());
$test->addTestCase(new Doctrine_Connection_Transaction_TestCase()); $test->addTestCase(new Doctrine_Connection_Transaction_TestCase());
......
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