Commit e75f3598 authored by pookey's avatar pookey

Relation fetching refactoring

parent 76081664
......@@ -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;
}
}
?>
......@@ -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 ||
......
......@@ -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()));
......
......@@ -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
*
......
......@@ -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");
......
......@@ -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());
......
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