diff --git a/Doctrine/Association.php b/Doctrine/Association.php index 951ae049c..16581da70 100644 --- a/Doctrine/Association.php +++ b/Doctrine/Association.php @@ -34,5 +34,27 @@ class Doctrine_Association extends Doctrine_Relation { public function getAssociationFactory() { return $this->associationTable; } + /** + * getRelationDql + * + * @param integer $count + * @return string + */ + public function getRelationDql($count, $context = 'record') { + $sub = "SELECT ".$this->foreign. + " FROM ".$this->associationTable->getTableName(). + " WHERE ".$this->local. + " IN (".substr(str_repeat("?, ", $count),0,-2).")"; + + $dql = "FROM ".$this->table->getComponentName(); + + if($context != 'record') + $dql .= ":".$this->associationTable->getComponentName(); + + $dql .= " WHERE ".$this->table->getComponentName().".".$this->table->getIdentifier(). + " IN ($sub)"; + + return $dql; + } } ?> diff --git a/Doctrine/Collection.php b/Doctrine/Collection.php index ecf4ca07f..7f8b7d9e0 100644 --- a/Doctrine/Collection.php +++ b/Doctrine/Collection.php @@ -488,13 +488,16 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator /** * loadRelated * - * @param string $name + * @param mixed $name */ - public function loadRelated($name) { + public function loadRelated($name = null) { + $query = new Doctrine_Query($this->table->getSession()); - $rel = $this->table->getForeignKey($name); - $table = $rel->getTable(); - $query = new Doctrine_Query($this->table->getSession()); + if( ! isset($name)) + return $query; + + $rel = $this->table->getForeignKey($name); + $table = $rel->getTable(); $foreign = $rel->getForeign(); $local = $rel->getLocal(); @@ -510,32 +513,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator $list[] = $value; endforeach; } - $paramStr = "(".substr(str_repeat("?, ", count($list)),0,-2).")"; - $multi = true; - - if($rel instanceof Doctrine_LocalKey || - $rel instanceof Doctrine_ForeignKey) - $dql = "FROM ".$table->getComponentName(). - " WHERE ".$table->getComponentName().".".$rel->getForeign(). - " IN ".$paramStr; - - - if($rel instanceof Doctrine_LocalKey) { - $multi = false; - } elseif($rel instanceof Doctrine_Association) { - $asf = $rel->getAssociationFactory(); - $sub = "SELECT ".$foreign. - " FROM ".$asf->getTableName(). - " WHERE ".$local. - " IN ".$paramStr; - - $table->getForeignKey($table->getAlias($this->table->getComponentName())); - - $dql = "FROM ".$table->getComponentName().":".$asf->getComponentName()." WHERE ".$table->getComponentName().".".$table->getIdentifier()." IN ($sub)"; - //$query->parseQuery($dql); - //print Doctrine_Lib::formatSql($query->getQuery()); - } - + $dql = $rel->getRelationDql(count($list), 'collection'); $coll = $query->query($dql, $list); @@ -567,6 +545,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator } } elseif($rel instanceof Doctrine_Association) { $identifier = $this->table->getIdentifier(); + $asf = $rel->getAssociationFactory(); foreach($this->data as $key => $record) { if($record->getState() == Doctrine_Record::STATE_TCLEAN || diff --git a/Doctrine/Record.php b/Doctrine/Record.php index 8886937d4..c206a6f64 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -1153,11 +1153,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } elseif($fk instanceof Doctrine_Association) { - $asf = $fk->getAssociationFactory(); - $query = "SELECT ".$foreign." FROM ".$asf->getTableName()." WHERE ".$local." = ?"; - - $graph = new Doctrine_Query($table->getSession()); - $query = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".".$table->getIdentifier()." IN ($query)"; + $query = $fk->getRelationDql(1); $coll = $graph->query($query, array($this->getIncremented())); diff --git a/Doctrine/Relation.php b/Doctrine/Relation.php index f29bd1641..694cd9d3f 100644 --- a/Doctrine/Relation.php +++ b/Doctrine/Relation.php @@ -52,23 +52,23 @@ class Doctrine_Relation { /** * @var Doctrine_Table $table foreign factory */ - private $table; + protected $table; /** * @var string $local local field */ - private $local; + protected $local; /** * @var string $foreign foreign field */ - private $foreign; + protected $foreign; /** * @var integer $type bind type */ - private $type; + protected $type; /** * @var string $alias relation alias */ - private $alias; + protected $alias; /** * @param Doctrine_Table $table @@ -115,7 +115,19 @@ class Doctrine_Relation { final public function getForeign() { return $this->foreign; } - + /** + * getRelationDql + * + * @param integer $count + * @return string + */ + public function getRelationDql($count) { + $dql = "FROM ".$this->table->getComponentName(). + " WHERE ".$this->table->getComponentName(). '.' . $this->foreign. + " IN (".substr(str_repeat("?, ", $count),0,-2).")"; + + return $dql; + } /** * getDeleteOperations * diff --git a/tests/CollectionTestCase.php b/tests/CollectionTestCase.php index c7f393a06..ca7078705 100644 --- a/tests/CollectionTestCase.php +++ b/tests/CollectionTestCase.php @@ -14,6 +14,13 @@ 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"); diff --git a/tests/run.php b/tests/run.php index 7a20b973d..06e73eb59 100644 --- a/tests/run.php +++ b/tests/run.php @@ -46,8 +46,6 @@ $test->addTestCase(new Doctrine_ConfigurableTestCase()); $test->addTestCase(new Doctrine_Collection_OffsetTestCase()); -$test->addTestCase(new Doctrine_CollectionTestCase()); - $test->addTestCase(new Doctrine_PessimisticLockingTestCase()); $test->addTestCase(new Doctrine_ViewTestCase()); @@ -65,6 +63,9 @@ $test->addTestCase(new Doctrine_Filter_TestCase()); $test->addTestCase(new Doctrine_ValueHolder_TestCase()); $test->addTestCase(new Doctrine_ValidatorTestCase()); + +$test->addTestCase(new Doctrine_CollectionTestCase()); + //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());