Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selection::page() counts with GROUP BY #193

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public function limit(int $limit, int $offset = null)
public function page(int $page, int $itemsPerPage, &$numOfPages = null)
{
if (func_num_args() > 2) {
$numOfPages = (int) ceil($this->count('*') / $itemsPerPage);
$numOfPages = (int) ceil($this->count() / $itemsPerPage);
}
if ($page < 1) {
$itemsPerPage = 0;
Expand Down
14 changes: 14 additions & 0 deletions tests/Database/Table/Selection.page().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ test(function () use ($context) { //less items than $itemsPerPage
$tags = $context->table('tag')->page(1, 100);
Assert::equal(4, count($tags)); //all four items from db
});

// SQL Server throw PDOException 'The number of rows provided for a FETCH clause must be greater then zero.'
if ($driverName !== 'sqlsrv') {
Assert::error(function () use ($context) { //invalid params
$tags = $context->table('tag')->page('foo', 'bar');
Assert::equal(0, count($tags)); //no items
}, PHP_VERSION_ID >= 70100 ? [[E_WARNING, 'A non-numeric value encountered']] : []);
}

test(function () use ($context) { // GROUP BY
$books = $context->table('book')->group('author_id')->page(2, 1, $numOfPages);
Assert::equal(1, count($books));
Assert::equal(2, $numOfPages);
});