diff --git a/classes/Collection.class.php b/classes/Collection.class.php index be27af3f8..537c1d9a5 100644 --- a/classes/Collection.class.php +++ b/classes/Collection.class.php @@ -119,9 +119,9 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator foreach($this->getNormalIterator() as $record) { if($value !== null) { - $record->set($this->reference_field, $value); + $record->rawSet($this->reference_field, $value); } else { - $record->set($this->reference_field, $this->reference); + $record->rawSet($this->reference_field, $this->reference); } } } @@ -284,9 +284,9 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator $value = $this->reference->get($this->relation->getLocal()); if($value !== null) { - $this->data[$key]->set($this->reference_field, $value); + $this->data[$key]->rawSet($this->reference_field, $value); } else { - $this->data[$key]->set($this->reference_field, $this->reference); + $this->data[$key]->rawSet($this->reference_field, $this->reference); } } } diff --git a/classes/Collection/Batch.class.php b/classes/Collection/Batch.class.php index 243a4e76a..ed5d0ce64 100644 --- a/classes/Collection/Batch.class.php +++ b/classes/Collection/Batch.class.php @@ -142,7 +142,7 @@ class Doctrine_Collection_Batch extends Doctrine_Collection { if(isset($this->reference_field)) - $this->data[$key]->set($this->reference_field,$this->reference); + $this->data[$key]->rawSet($this->reference_field,$this->reference); return $this->data[$key]; diff --git a/classes/DQL/Parser.class.php b/classes/DQL/Parser.class.php index f5a227829..e54ff38c5 100644 --- a/classes/DQL/Parser.class.php +++ b/classes/DQL/Parser.class.php @@ -829,3 +829,5 @@ class Doctrine_DQL_Parser { } ?> + + diff --git a/classes/Record.class.php b/classes/Record.class.php index 808f38732..f9fdba305 100644 --- a/classes/Record.class.php +++ b/classes/Record.class.php @@ -87,10 +87,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @var integer $oid object identifier */ private $oid; - /** - * @var boolean $loaded whether or not this object has its data loaded from database - */ - private $loaded = false; /** * constructor @@ -149,8 +145,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if($cols <= 1) $this->state = Doctrine_Record::STATE_PROXY; - else - $this->loaded = true; $this->prepareIdentifiers(); @@ -177,14 +171,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite public function getOID() { return $this->oid; } - /** - * isLoaded - * whether or not this object has been fully loaded - * @return boolean - */ - public function isLoaded() { - return $this->loaded; - } /** * cleanData * modifies data array @@ -245,7 +231,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite unset($this->references); unset($this->originals); unset($this->oid); - unset($this->loaded); foreach($this->data as $k=>$v) { if($v instanceof Doctrine_Record) @@ -273,8 +258,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->table->getRepository()->add($this); - $this->loaded = true; - $this->cleanData(); //unset($this->data['id']); @@ -335,7 +318,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->prepareIdentifiers(); - $this->loaded = true; $this->state = Doctrine_Record::STATE_CLEAN; return true; @@ -360,7 +342,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->state = Doctrine_Record::STATE_CLEAN; $this->modified = array(); - $this->loaded = true; } /** * return the factory that created this data access object @@ -389,29 +370,25 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite // check if the property is not loaded (= it is an empty array) if(is_array($this->data[$name])) { - - if( ! $this->loaded) { - - // no use trying to load the data from database if the Doctrine_Record is new or clean - if($this->state != Doctrine_Record::STATE_TDIRTY && - $this->state != Doctrine_Record::STATE_TCLEAN && - $this->state != Doctrine_Record::STATE_CLEAN) { - if( ! empty($this->collections)) { - foreach($this->collections as $collection) { - $collection->load($this); - } - } else { - $this->refresh(); + // no use trying to load the data from database if the Doctrine_Record is not a proxy + if($this->state != Doctrine_Record::STATE_TDIRTY && + $this->state != Doctrine_Record::STATE_TCLEAN && + $this->state != Doctrine_Record::STATE_CLEAN && + $this->state != Doctrine_Record::STATE_DIRTY) { + + if( ! empty($this->collections)) { + foreach($this->collections as $collection) { + $collection->load($this); } - $this->state = Doctrine_Record::STATE_CLEAN; + } else { + $this->refresh(); } - $this->loaded = true; + $this->state = Doctrine_Record::STATE_CLEAN; } - + if(is_array($this->data[$name])) return null; - } return $this->data[$name]; } @@ -437,8 +414,22 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if( ! empty($id)) $value = $id; - - $this->data[$name] = $value; + + if(isset($this->data[$name])) { + if( ! is_array($this->data[$name])) { + if($this->data[$name] !== $value) { + switch($this->state): + case Doctrine_Record::STATE_CLEAN: + $this->state = Doctrine_Record::STATE_DIRTY; + break; + case Doctrine_Record::STATE_TCLEAN: + $this->state = Doctrine_Record::STATE_TDIRTY; + endswitch; + } + } + $this->data[$name] = $value; + $this->modified[] = $name; + } } /** * set @@ -452,8 +443,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite */ public function set($name,$value) { if(isset($this->data[$name])) { - $old = $this->get($name); - if($value instanceof Doctrine_Record) { $id = $value->getID(); @@ -461,13 +450,12 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite if( ! empty($id)) $value = $value->getID(); } + + $old = $this->get($name); if($old !== $value) { $this->data[$name] = $value; - $this->modified[] = $name; - - switch($this->state): case Doctrine_Record::STATE_CLEAN: case Doctrine_Record::STATE_PROXY: diff --git a/tests/DQLParserTestCase.class.php b/tests/DQLParserTestCase.class.php index de2218817..1a4ccb2fd 100644 --- a/tests/DQLParserTestCase.class.php +++ b/tests/DQLParserTestCase.class.php @@ -207,3 +207,5 @@ class Doctrine_DQL_ParserTestCase extends Doctrine_UnitTestCase { } ?> + + diff --git a/tests/RecordTestCase.class.php b/tests/RecordTestCase.class.php index d334c1330..5be32d83d 100644 --- a/tests/RecordTestCase.class.php +++ b/tests/RecordTestCase.class.php @@ -2,6 +2,43 @@ require_once("UnitTestCase.class.php"); class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { + public function testManyToManyTreeStructure() { + $task = $this->session->create("Task"); + + $task->name = "Task 1"; + $task->Resource[0]->name = "Resource 1"; + + $this->session->flush(); + $this->assertEqual($this->dbh->query("SELECT COUNT(*) FROM assignment")->fetch(PDO::FETCH_NUM),array(1)); + + $task = new Task(); + $this->assertTrue($task instanceof Task); + $this->assertEqual($task->getState(), Doctrine_Record::STATE_TCLEAN); + $this->assertTrue($task->Task[0] instanceof Task); + + $this->assertEqual($task->Task[0]->getState(), Doctrine_Record::STATE_TCLEAN); + $this->assertTrue($task->Resource[0] instanceof Resource); + $this->assertEqual($task->Resource[0]->getState(), Doctrine_Record::STATE_TCLEAN); + + $task->name = "Task 1"; + $task->Resource[0]->name = "Resource 1"; + $task->Task[0]->name = "Subtask 1"; + + $this->assertEqual($task->name, "Task 1"); + $this->assertEqual($task->Resource[0]->name, "Resource 1"); + $this->assertEqual($task->Resource->count(), 1); + $this->assertEqual($task->Task[0]->name, "Subtask 1"); + + $this->session->flush(); + + $task = $task->getTable()->find($task->getID()); + + $this->assertEqual($task->name, "Task 1"); + $this->assertEqual($task->Resource[0]->name, "Resource 1"); + $this->assertEqual($task->Resource->count(), 1); + $this->assertEqual($task->Task[0]->name, "Subtask 1"); + + } public function testOne2OneForeign() { diff --git a/tests/UnitTestCase.class.php b/tests/UnitTestCase.class.php index f9c2f1e92..f2690f6ea 100644 --- a/tests/UnitTestCase.class.php +++ b/tests/UnitTestCase.class.php @@ -32,7 +32,7 @@ class Doctrine_UnitTestCase extends UnitTestCase { $this->manager->setAttribute(Doctrine::ATTR_CACHE, Doctrine::CACHE_NONE); $this->manager->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE); - $this->tables = array("entity","email","phonenumber","groupuser","album","song","element","error","description","address","account"); + $this->tables = array("entity","email","phonenumber","groupuser","album","song","element","error","description","address","account","task","resource","assignment"); $tables = $this->tables; diff --git a/tests/ValidatorTestCase.class.php b/tests/ValidatorTestCase.class.php index d04c0bd92..d3b6fe5fb 100644 --- a/tests/ValidatorTestCase.class.php +++ b/tests/ValidatorTestCase.class.php @@ -10,7 +10,6 @@ class Doctrine_ValidatorTestCase extends Doctrine_UnitTestCase { $email = $this->old->Email; $email->address = "zYne@invalid"; - $this->assertTrue($this->old->isLoaded()); $this->assertTrue($this->old->getModified() == $set); $validator = new Doctrine_Validator(); diff --git a/tests/classes.php b/tests/classes.php index 73eea2f40..7820593d7 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -124,4 +124,31 @@ class Song extends Doctrine_Record { $this->hasColumn("title","string",30); } } + +class Task extends Doctrine_Record { + public function setUp() { + $this->hasMany("Resource","Assignment.resource_id"); + $this->hasMany("Task","Task.parent_id"); + } + public function setTableDefinition() { + $this->hasColumn("name","string",100); + $this->hasColumn("parent_id","integer"); + } +} + +class Resource extends Doctrine_Record { + public function setUp() { + $this->hasMany("Task","Assignment.task_id"); + } + public function setTableDefinition() { + $this->hasColumn("name","string",100); + } +} + +class Assignment extends Doctrine_Record { + public function setTableDefinition() { + $this->hasColumn("task_id","integer"); + $this->hasColumn("resource_id","integer"); + } +} ?> diff --git a/tests/run.php b/tests/run.php index 03e0e1a25..f559c0c79 100644 --- a/tests/run.php +++ b/tests/run.php @@ -24,12 +24,16 @@ error_reporting(E_ALL); $test = new GroupTest("Doctrine Framework Unit Tests"); -$test->addTestCase(new Doctrine_TableTestCase()); + +$test->addTestCase(new Doctrine_RecordTestCase()); + +$test->addTestCase(new Doctrine_TableTestCase()); + $test->addTestCase(new Doctrine_SessionTestCase()); -$test->addTestCase(new Doctrine_RecordTestCase()); +$test->addTestCase(new Doctrine_DQL_ParserTestCase()); $test->addTestCase(new Doctrine_ValidatorTestCase()); @@ -44,7 +48,7 @@ $test->addTestCase(new Doctrine_EventListenerTestCase()); $test->addTestCase(new Doctrine_BatchIteratorTestCase()); -$test->addTestCase(new Doctrine_DQL_ParserTestCase()); + $test->addTestCase(new Doctrine_ConfigurableTestCase());