Commit 9bd341d7 authored by zYne's avatar zYne

Doctrine_Record::countRelated() added

parent 5b0858cf
......@@ -713,7 +713,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
if( ! isset($this->tables[$tableName])) {
$this->tables[$tableName] = $table;
if($loadFields && ! $this->aggregate) {
if($loadFields) {
$this->parseFields($fullname, $tableName, $e2, $currPath);
}
}
......@@ -767,7 +767,7 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable {
}
if( ! $this->aggregate)
$this->loadFields($table, $fetchmode, $fields, $currPath);
$this->loadFields($table, $fetchmode, $fields, $currPath);
}
public function parseAggregateFunction($func,$reference) {
$pos = strpos($func,"(");
......
......@@ -1262,12 +1262,17 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
}
/**
* countRelated
*
* @return integer
*/
public function countRelated($name) {
$rel = $this->table->getForeignKey($name);
$componentName = $rel->getTable()->getTableName();
return $rel->getCountFor($this);
$rel = $this->table->getForeignKey($name);
$componentName = $rel->getTable()->getComponentName();
$alias = $rel->getTable()->getAlias(get_class($this));
$query = new Doctrine_Query();
$query->from($componentName. '(' . 'COUNT(1)' . ')')->where($componentName. '.' .$alias. '.' . $this->getTable()->getIdentifier(). ' = ?');
$array = $query->execute(array($this->getIncremented()));
return $array[0]['COUNT(1)'];
}
/**
* merge
......
......@@ -25,6 +25,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
}
public function testSelectingAggregateValues() {
$q = new Doctrine_Query();
$q->from("User(COUNT(1), MAX(name))");
$array = $q->execute();
......@@ -34,7 +35,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$q = new Doctrine_Query();
$q->from("Phonenumber(COUNT(1))");
$array = $q->execute();
$this->assertTrue(is_array($array));
$this->assertEqual($array, array(array('COUNT(1)' => '15')));
......@@ -52,10 +53,20 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
$q->from("User(MAX(id)).Email(MIN(address))");
$array = $q->execute();
$this->assertTrue(is_array($array));
$this->assertEqual($array[0]['MAX(entity.id)'], 11);
$this->assertEqual($array[0]['MIN(email.address)'], 'arnold@example.com');
$q = new Doctrine_Query();
$q->from("User(MAX(id)).Email(MIN(address)), User.Phonenumber(COUNT(1))");
$array = $q->execute();
$this->assertTrue(is_array($array));
$this->assertEqual($array[0]['MAX(entity.id)'], 11);
$this->assertEqual($array[0]['MIN(email.address)'], 'arnold@example.com');
$this->assertEqual($array[0]['COUNT(1)'], 14);
}
public function testMultipleFetching() {
$count = $this->dbh->count();
$this->connection->getTable('User')->clear();
......@@ -1213,6 +1224,5 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase {
//$this->assertTrue(isset($values['max']));
}
}
?>
......@@ -93,6 +93,22 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase {
$this->assertEqual($coll->count(), 1);
}
public function testCountRelated() {
$user = $this->connection->getTable('Entity')->find(5);
$c = $user->countRelated('Phonenumber');
$this->assertEqual($c, 3);
$user = $this->connection->getTable('Entity')->find(7);
$c = $user->countRelated('Phonenumber');
$this->assertEqual($c, 1);
$user = $this->connection->getTable('Entity')->find(8);
$c = $user->countRelated('Phonenumber');
$this->assertEqual($c, 3);
}
public function testUpdatingWithNullValue() {
$user = $this->connection->getTable('User')->find(5);
$user->name = null;
......
......@@ -97,6 +97,9 @@ class Phonenumber extends Doctrine_Record {
$this->hasColumn("phonenumber","string",20);
$this->hasColumn("entity_id","integer");
}
public function setUp() {
$this->hasOne("Entity", "Phonenumber.entity_id");
}
}
class Element extends Doctrine_Record {
......
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