Skip to content
Permalink
Browse files

Add total() to ORM\Query.

This method makes it much easier to get COUNT(*) for queries. This will
make removing paginateCount() from PaginatorComponent.
  • Loading branch information...
markstory committed Nov 16, 2013
1 parent 759dfeb commit 613a25aa4f614e48a7cd982eeff73e2dd4b35371
Showing with 33 additions and 0 deletions.
  1. +14 −0 Cake/ORM/Query.php
  2. +19 −0 Cake/Test/TestCase/ORM/QueryTest.php
@@ -616,6 +616,20 @@ public function first() {
return $this->_results->one();
}
/**
* Return the COUNT(*) for for the query.
*
* This method will replace the selected fields with a COUNT(*)
* and execute the queries returning the number of rows.
*
* @return integer
*/
public function total() {
return $this->select(['count' => $this->count('*')], true)
->hydrate(false)
->first()['count'];
}
/**
* Toggle hydrating entites.
*
@@ -1351,4 +1351,23 @@ public function testHydrateBelongsToCustomEntity() {
$this->assertInstanceOf($authorEntity, $first->author);
}
/**
* Test getting totals from queries.
*
* @return void
*/
public function testTotal() {
$table = TableRegistry::get('articles');
$result = $table->find('all')->total();
$this->assertEquals(3, $result);
$query = $table->find('all')
->where(['id >' => 1]);
$result = $query->total();
$this->assertEquals(2, $result);
$result = $query->execute();
$this->assertEquals(['count' => 2], $result->one());
}
}

0 comments on commit 613a25a

Please sign in to comment.
You can’t perform that action at this time.