From b022675ab771d95ed91ae21e6f32faf7cb3e3edf Mon Sep 17 00:00:00 2001 From: pookey Date: Tue, 15 Aug 2006 21:33:41 +0000 Subject: [PATCH] little refactorings --- Doctrine/Collection.php | 27 +++++++++++-- Doctrine/Record.php | 76 ++++++------------------------------ tests/CollectionTestCase.php | 21 +++++++--- tests/run.php | 3 ++ 4 files changed, 55 insertions(+), 72 deletions(-) diff --git a/Doctrine/Collection.php b/Doctrine/Collection.php index 7f8b7d9e0..49a113af0 100644 --- a/Doctrine/Collection.php +++ b/Doctrine/Collection.php @@ -493,8 +493,17 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator public function loadRelated($name = null) { $query = new Doctrine_Query($this->table->getSession()); - if( ! isset($name)) + if( ! isset($name)) { + foreach($this->data as $record): + $value = $record->getIncremented(); + if($value !== null) + $list[] = $value; + endforeach; + $query->from($this->table->getComponentName()."(".implode(", ",$this->table->getPrimaryKeys()).")"); + $query->where($this->table->getComponentName().".id IN (".substr(str_repeat("?, ", count($list)),0,-2).")"); + return $query; + } $rel = $this->table->getForeignKey($name); $table = $rel->getTable(); @@ -516,7 +525,20 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator $dql = $rel->getRelationDql(count($list), 'collection'); $coll = $query->query($dql, $list); - + $this->populateRelated($name, $coll); + } + /** + * populateRelated + * + * @param string $name + * @param Doctrine_Collection $coll + * @return void + */ + public function populateRelated($name, Doctrine_Collection $coll) { + $rel = $this->table->getForeignKey($name); + $table = $rel->getTable(); + $foreign = $rel->getForeign(); + $local = $rel->getLocal(); if($rel instanceof Doctrine_LocalKey) { foreach($this->data as $key => $record) { @@ -565,7 +587,6 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator $this->data[$key]->setRelated($name, $sub); } } - } /** * getNormalIterator diff --git a/Doctrine/Record.php b/Doctrine/Record.php index c206a6f64..bb09c45f5 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -1140,27 +1140,19 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } break; default: + $query = $fk->getRelationDql(1); + // ONE-TO-MANY if($fk instanceof Doctrine_ForeignKey) { $id = $this->get($local); - $query = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".".$fk->getForeign()." = ?"; - $coll = $graph->query($query,array($id)); - - $this->references[$name] = $coll; - $this->references[$name]->setReference($this, $fk); - - $this->originals[$name] = clone $coll; - + $coll = $graph->query($query,array($id)); + $coll->setReference($this, $fk); } elseif($fk instanceof Doctrine_Association) { - - $query = $fk->getRelationDql(1); - - $coll = $graph->query($query, array($this->getIncremented())); - - $this->references[$name] = $coll; - $this->originals[$name] = clone $coll; - + $id = $this->getIncremented(); + $coll = $graph->query($query, array($id)); } + $this->references[$name] = $coll; + $this->originals[$name] = clone $coll; endswitch; break; endswitch; @@ -1179,17 +1171,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite return $this->filters[$componentAlias]; } - /** - * sets enumerated value array for given field - * - * @param string $field - * @param array $values - * @return void - */ - final public function setEnumValues($field, array $values) { - $this->table->setEnumValues($field, $values); - } - /** * binds One-to-One composite relation * @@ -1230,14 +1211,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite final public function hasMany($componentName,$foreignKey, $localKey = null) { $this->table->bind($componentName,$foreignKey,Doctrine_Relation::MANY_AGGREGATE, $localKey); } - /** - * setInheritanceMap - * @param array $inheritanceMap - * @return void - */ - final public function setInheritanceMap(array $inheritanceMap) { - $this->table->setInheritanceMap($inheritanceMap); - } /** * setPrimaryKey * @param mixed $key @@ -1245,24 +1218,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite final public function setPrimaryKey($key) { $this->table->setPrimaryKey($key); } - /** - * setTableName - * @param string $name table name - * @return void - */ - final public function setTableName($name) { - $this->table->setTableName($name); - } - /** - * setAttribute - * @param integer $attribute - * @param mixed $value - * @see Doctrine::ATTR_* constants - * @return void - */ - final public function setAttribute($attribute, $value) { - $this->table->setAttribute($attribute,$value); - } /** * hasColumn * sets a column definition @@ -1294,6 +1249,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite * @param array $a */ public function __call($m,$a) { + if(method_exists($this->table, $m)) + return call_user_func_array(array($this->table, $m), $a); + if( ! function_exists($m)) throw new Doctrine_Record_Exception("unknown callback '$m'"); @@ -1302,16 +1260,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $a[0] = $this->get($column); $newvalue = call_user_func_array($m, $a); - - /** - if( ! isset($a[1]) || $a[1] == Doctrine_Record::CALLBACK_RAW) { - */ - $this->data[$column] = $newvalue; - /** - } elseif($a[1] == Doctrine_Record::CALLBACK_STATEWISE) { - $this->set($column, call_user_func_array($m, $a)); - } - */ + + $this->data[$column] = $newvalue; } return $this; } diff --git a/tests/CollectionTestCase.php b/tests/CollectionTestCase.php index ca7078705..bc42d304a 100644 --- a/tests/CollectionTestCase.php +++ b/tests/CollectionTestCase.php @@ -14,13 +14,7 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { $this->assertTrue($coll->count(),3); $this->assertEqual($coll->getKeys(), array(0,1,2)); } - public function testLoadRelated() { - $coll = $this->session->query("FROM User"); - $q = $coll->loadRelated(); - - $this->assertTrue($q instanceof Doctrine_Query); - } public function testLoadRelatedForAssociation() { $coll = $this->session->query("FROM User"); @@ -75,7 +69,22 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { $this->session->clear(); } + public function testLoadRelated() { + $coll = $this->session->query("FROM User(id)"); + $q = $coll->loadRelated(); + + $this->assertTrue($q instanceof Doctrine_Query); + + $q->addFrom('User.Group'); + + $coll2 = $q->execute($coll->getPrimaryKeys()); + $this->assertEqual($coll2->count(), $coll->count()); + + $count = $this->dbh->count(); + $coll[0]->Group[0]; + $this->assertEqual($count, $this->dbh->count()); + } public function testLoadRelatedForLocalKeyRelation() { $coll = $this->session->query("FROM User"); diff --git a/tests/run.php b/tests/run.php index 06e73eb59..885e32cfb 100644 --- a/tests/run.php +++ b/tests/run.php @@ -23,6 +23,7 @@ require_once("RawSqlTestCase.php"); require_once("CustomPrimaryKeyTestCase.php"); require_once("FilterTestCase.php"); require_once("ValueHolderTestCase.php"); +require_once("QueryLimitTestCase.php"); error_reporting(E_ALL); @@ -66,6 +67,8 @@ $test->addTestCase(new Doctrine_ValidatorTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase()); +$test->addTestCase(new Doctrine_Query_Limit_TestCase()); + //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());