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