From 9bd341d70834441044b7365d5872aea1c53ee520 Mon Sep 17 00:00:00 2001 From: zYne Date: Fri, 25 Aug 2006 23:50:55 +0000 Subject: [PATCH] Doctrine_Record::countRelated() added --- Doctrine/Query.php | 4 ++-- Doctrine/Record.php | 13 +++++++++---- tests/QueryTestCase.php | 14 ++++++++++++-- tests/RecordTestCase.php | 16 ++++++++++++++++ tests/classes.php | 3 +++ 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/Doctrine/Query.php b/Doctrine/Query.php index ac01b0db6..695f27acf 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -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,"("); diff --git a/Doctrine/Record.php b/Doctrine/Record.php index 20d902166..622c36df8 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -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 diff --git a/tests/QueryTestCase.php b/tests/QueryTestCase.php index 7420cd588..08eedc3a8 100644 --- a/tests/QueryTestCase.php +++ b/tests/QueryTestCase.php @@ -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'])); } - } ?> diff --git a/tests/RecordTestCase.php b/tests/RecordTestCase.php index 9d9d205ae..57459041d 100644 --- a/tests/RecordTestCase.php +++ b/tests/RecordTestCase.php @@ -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; diff --git a/tests/classes.php b/tests/classes.php index d067af5ff..60b054b62 100644 --- a/tests/classes.php +++ b/tests/classes.php @@ -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 {