Commit b022675a authored by pookey's avatar pookey

little refactorings

parent e75f3598
...@@ -493,8 +493,17 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -493,8 +493,17 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
public function loadRelated($name = null) { public function loadRelated($name = null) {
$query = new Doctrine_Query($this->table->getSession()); $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; return $query;
}
$rel = $this->table->getForeignKey($name); $rel = $this->table->getForeignKey($name);
$table = $rel->getTable(); $table = $rel->getTable();
...@@ -516,7 +525,20 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -516,7 +525,20 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$dql = $rel->getRelationDql(count($list), 'collection'); $dql = $rel->getRelationDql(count($list), 'collection');
$coll = $query->query($dql, $list); $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) { if($rel instanceof Doctrine_LocalKey) {
foreach($this->data as $key => $record) { foreach($this->data as $key => $record) {
...@@ -565,7 +587,6 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -565,7 +587,6 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$this->data[$key]->setRelated($name, $sub); $this->data[$key]->setRelated($name, $sub);
} }
} }
} }
/** /**
* getNormalIterator * getNormalIterator
......
...@@ -1140,27 +1140,19 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1140,27 +1140,19 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
} }
break; break;
default: default:
$query = $fk->getRelationDql(1);
// ONE-TO-MANY // ONE-TO-MANY
if($fk instanceof Doctrine_ForeignKey) { if($fk instanceof Doctrine_ForeignKey) {
$id = $this->get($local); $id = $this->get($local);
$query = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".".$fk->getForeign()." = ?"; $coll = $graph->query($query,array($id));
$coll = $graph->query($query,array($id)); $coll->setReference($this, $fk);
$this->references[$name] = $coll;
$this->references[$name]->setReference($this, $fk);
$this->originals[$name] = clone $coll;
} elseif($fk instanceof Doctrine_Association) { } elseif($fk instanceof Doctrine_Association) {
$id = $this->getIncremented();
$query = $fk->getRelationDql(1); $coll = $graph->query($query, array($id));
$coll = $graph->query($query, array($this->getIncremented()));
$this->references[$name] = $coll;
$this->originals[$name] = clone $coll;
} }
$this->references[$name] = $coll;
$this->originals[$name] = clone $coll;
endswitch; endswitch;
break; break;
endswitch; endswitch;
...@@ -1179,17 +1171,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1179,17 +1171,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
return $this->filters[$componentAlias]; 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 * binds One-to-One composite relation
* *
...@@ -1230,14 +1211,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1230,14 +1211,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
final public function hasMany($componentName,$foreignKey, $localKey = null) { final public function hasMany($componentName,$foreignKey, $localKey = null) {
$this->table->bind($componentName,$foreignKey,Doctrine_Relation::MANY_AGGREGATE, $localKey); $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 * setPrimaryKey
* @param mixed $key * @param mixed $key
...@@ -1245,24 +1218,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1245,24 +1218,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
final public function setPrimaryKey($key) { final public function setPrimaryKey($key) {
$this->table->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 * hasColumn
* sets a column definition * sets a column definition
...@@ -1294,6 +1249,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1294,6 +1249,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* @param array $a * @param array $a
*/ */
public function __call($m,$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)) if( ! function_exists($m))
throw new Doctrine_Record_Exception("unknown callback '$m'"); throw new Doctrine_Record_Exception("unknown callback '$m'");
...@@ -1302,16 +1260,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1302,16 +1260,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$a[0] = $this->get($column); $a[0] = $this->get($column);
$newvalue = call_user_func_array($m, $a); $newvalue = call_user_func_array($m, $a);
/** $this->data[$column] = $newvalue;
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));
}
*/
} }
return $this; return $this;
} }
......
...@@ -14,13 +14,7 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { ...@@ -14,13 +14,7 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase {
$this->assertTrue($coll->count(),3); $this->assertTrue($coll->count(),3);
$this->assertEqual($coll->getKeys(), array(0,1,2)); $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() { public function testLoadRelatedForAssociation() {
$coll = $this->session->query("FROM User"); $coll = $this->session->query("FROM User");
...@@ -75,7 +69,22 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase { ...@@ -75,7 +69,22 @@ class Doctrine_CollectionTestCase extends Doctrine_UnitTestCase {
$this->session->clear(); $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() { public function testLoadRelatedForLocalKeyRelation() {
$coll = $this->session->query("FROM User"); $coll = $this->session->query("FROM User");
......
...@@ -23,6 +23,7 @@ require_once("RawSqlTestCase.php"); ...@@ -23,6 +23,7 @@ require_once("RawSqlTestCase.php");
require_once("CustomPrimaryKeyTestCase.php"); require_once("CustomPrimaryKeyTestCase.php");
require_once("FilterTestCase.php"); require_once("FilterTestCase.php");
require_once("ValueHolderTestCase.php"); require_once("ValueHolderTestCase.php");
require_once("QueryLimitTestCase.php");
error_reporting(E_ALL); error_reporting(E_ALL);
...@@ -66,6 +67,8 @@ $test->addTestCase(new Doctrine_ValidatorTestCase()); ...@@ -66,6 +67,8 @@ $test->addTestCase(new Doctrine_ValidatorTestCase());
$test->addTestCase(new Doctrine_CollectionTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase());
$test->addTestCase(new Doctrine_Query_Limit_TestCase());
//$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase());
//$test->addTestCase(new Doctrine_Cache_SqliteTestCase()); //$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