Skip to content

Commit f17ad0a

Browse files
authored
Merge pull request #1217 from nowackipawel/patch-8
Optional parameter for resetSelect() call in Builder's countAll();
2 parents 4d61afd + b6fa847 commit f17ad0a

3 files changed

Lines changed: 38 additions & 14 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,19 +1386,23 @@ public function getCompiledSelect($reset = true)
13861386
*
13871387
* @param int $limit The limit clause
13881388
* @param int $offset The offset clause
1389-
* @param bool $returnSQL If true, returns the generate SQL, otherwise executes the query.
1389+
* @param bool $returnSQL If true, returns the generate SQL, otherwise executes the query.
1390+
* @param bool $reset Are we want to clear query builder values?
13901391
*
13911392
* @return ResultInterface
13921393
*/
1393-
public function get(int $limit = null, int $offset = 0, $returnSQL = false)
1394+
public function get(int $limit = null, int $offset = 0, $returnSQL = false, $reset = true)
13941395
{
13951396
if ( ! is_null($limit))
13961397
{
13971398
$this->limit($limit, $offset);
13981399
}
13991400
$result = $returnSQL ? $this->getCompiledSelect() : $this->db->query($this->compileSelect(), $this->binds);
14001401

1401-
$this->resetSelect();
1402+
if($reset == true)
1403+
{
1404+
$this->resetSelect();
1405+
}
14021406

14031407
return $result;
14041408
}
@@ -1411,11 +1415,12 @@ public function get(int $limit = null, int $offset = 0, $returnSQL = false)
14111415
* Generates a platform-specific query string that counts all records in
14121416
* the specified database
14131417
*
1414-
* @param bool $test Are we running automated tests?
1418+
* @param bool $reset Are we want to clear query builder values?
1419+
* @param bool $test Are we running automated tests?
14151420
*
14161421
* @return int
14171422
*/
1418-
public function countAll($test = false)
1423+
public function countAll($reset = true, $test = false)
14191424
{
14201425
$table = $this->QBFrom[0];
14211426

@@ -1434,7 +1439,11 @@ public function countAll($test = false)
14341439
}
14351440

14361441
$query = $query->getRow();
1437-
$this->resetSelect();
1442+
1443+
if ($reset === true)
1444+
{
1445+
$this->resetSelect();
1446+
}
14381447

14391448
return (int) $query->numrows;
14401449
}
@@ -1457,6 +1466,7 @@ public function countAllResults($reset = true, $test = false)
14571466
// ORDER BY usage is often problematic here (most notably
14581467
// on Microsoft SQL Server) and ultimately unnecessary
14591468
// for selecting COUNT(*) ...
1469+
$orderby = [];
14601470
if ( ! empty($this->QBOrderBy))
14611471
{
14621472
$orderby = $this->QBOrderBy;
@@ -1480,7 +1490,7 @@ public function countAllResults($reset = true, $test = false)
14801490
// If we've previously reset the QBOrderBy values, get them back
14811491
elseif ( ! isset($this->QBOrderBy))
14821492
{
1483-
$this->QBOrderBy = $orderby;
1493+
$this->QBOrderBy = $orderby??[];
14841494
}
14851495

14861496
$row = (! $result instanceof ResultInterface)

tests/system/Database/Builder/CountTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function testCountAll()
2424

2525
$expectedSQL = "SELECT COUNT(*) AS \"numrows\" FROM \"jobs\"";
2626

27-
$this->assertEquals($expectedSQL, $builder->countAll(true));
27+
$this->assertEquals($expectedSQL, $builder->countAll(true, true));
2828
}
2929

3030
//--------------------------------------------------------------------
@@ -33,7 +33,7 @@ public function testCountAllResults()
3333
{
3434
$builder = new BaseBuilder('jobs', $this->db);
3535

36-
$answer = $builder->where('id >', 3)->countAllResults(null, true);
36+
$answer = $builder->where('id >', 3)->countAllResults(false, true);
3737

3838
$expectedSQL = "SELECT COUNT(*) AS \"numrows\" FROM \"jobs\" WHERE \"id\" > :id:";
3939

user_guide_src/source/database/query_builder.rst

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -511,29 +511,34 @@ The second parameter lets you set a result offset.
511511

512512
$builder->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
513513

514+
514515
**$builder->countAllResults()**
515516

516517
Permits you to determine the number of rows in a particular Query
517518
Builder query. Queries will accept Query Builder restrictors such as
518519
``where()``, ``orWhere()``, ``like()``, ``orLike()``, etc. Example::
519520

520-
echo $builder->countAllResults('my_table'); // Produces an integer, like 25
521+
echo $builder->countAllResults(); // Produces an integer, like 25
521522
$builder->like('title', 'match');
522523
$builder->from('my_table');
523524
echo $builder->countAllResults(); // Produces an integer, like 17
524525

525526
However, this method also resets any field values that you may have passed
526527
to ``select()``. If you need to keep them, you can pass ``FALSE`` as the
527-
second parameter::
528+
first parameter.
528529

529-
echo $builder->countAllResults('my_table', FALSE);
530+
echo $builder->countAllResults(false); // Produces an integer, like 17
530531

531532
**$builder->countAll()**
532533

533534
Permits you to determine the number of rows in a particular table.
534-
Submit the table name in the first parameter. Example::
535+
Example::
535536

536-
echo $builder->countAll('my_table'); // Produces an integer, like 25
537+
echo $builder->countAll(); // Produces an integer, like 25
538+
539+
As is in countAllResult method, this method resets any field values that you may have passed
540+
to ``select()`` as well. If you need to keep them, you can pass ``FALSE`` as the
541+
first parameter.
537542

538543
**************
539544
Query grouping
@@ -994,6 +999,15 @@ Class Reference
994999
Generates a platform-specific query string that counts
9951000
all records returned by an Query Builder query.
9961001

1002+
.. php:method:: countAll([$reset = TRUE])
1003+
1004+
:param bool $reset: Whether to reset values for SELECTs
1005+
:returns: Number of rows in the query result
1006+
:rtype: int
1007+
1008+
Generates a platform-specific query string that counts
1009+
all records returned by an Query Builder query.
1010+
9971011
.. php:method:: get([$limit = NULL[, $offset = NULL]])
9981012
9991013
:param int $limit: The LIMIT clause

0 commit comments

Comments
 (0)