Skip to content

Commit

Permalink
Merge pull request #13494 from niden/4.0.x
Browse files Browse the repository at this point in the history
[#13492] Changes to the paginator
  • Loading branch information
niden authored Oct 10, 2018
2 parents 06d63ad + 6ec3df6 commit eb26399
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Added `Phalcon\Db\Adapter\Pdo\Postgresql::describeReferences` to implement custom Postgresql rules
- Added `Phalcon\Mvc\Router\RouteInterface::convert` so that calling `Phalcon\Mvc\Router\Group::add` will return an instance that has `convert` method [#13380](https://github.com/phalcon/cphalcon/issues/13380)
- Added `Phalcon\Mvc\ModelInterface::getModelsMetaData` [#13070](https://github.com/phalcon/cphalcon/issues/13402)
- Added `paginate()` method as a proxy of `getPaginate`. Added `previous` in the paginator object same as `before`. After 4.0 is released we will deprecate `getPaginate()`, `before` and `total_items` [#13492](https://github.com/phalcon/cphalcon/issues/13492)

## Changed
- By configuring `prefix` and `statsKey` the `Phalcon\Cache\Backend\Redis::queryKeys` no longer returns prefixed keys, now it returns original keys without prefix. [#13456](https://github.com/phalcon/cphalcon/pull/13456)
Expand Down
16 changes: 8 additions & 8 deletions phalcon/paginator/adapter.zep
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ abstract class Adapter implements AdapterInterface
*/
protected _page = null;

/**
* Get current rows limit
*/
public function getLimit() -> int
{
return this->_limitRows;
}

/**
* Set the current page number
*/
Expand All @@ -52,12 +60,4 @@ abstract class Adapter implements AdapterInterface
let this->_limitRows = limitRows;
return this;
}

/**
* Get current rows limit
*/
public function getLimit() -> int
{
return this->_limitRows;
}
}
25 changes: 21 additions & 4 deletions phalcon/paginator/adapter/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,22 @@ class Model extends Adapter

/**
* Returns a slice of the resultset to show in the pagination
*
* @deprecated will be removed after 4.0
*/
public function getPaginate() -> <\stdClass>
{
return this->paginate();
}

/**
* Returns a slice of the resultset to show in the pagination
*/
public function paginate() -> <\stdClass>
{
var config, items, pageItems, page;
int pageNumber, show, n, start, lastShowPage,
i, next, totalPages, before;
i, next, totalPages, previous;

let show = (int) this->_limitRows,
config = this->_config,
Expand Down Expand Up @@ -136,18 +146,25 @@ class Model extends Adapter
}

if pageNumber > 1 {
let before = pageNumber - 1;
let previous = pageNumber - 1;
} else {
let before = 1;
let previous = 1;
}

let page = new \stdClass(),
page->items = pageItems,
page->first = 1,
page->before = before,
/**
* @deprecated `before` will be removed after 4.0
*/
page->before = previous,
page->previous = previous,
page->current = pageNumber,
page->last = totalPages,
page->next = next,
/**
* @deprecated `total_pages` will be removed after 4.0
*/
page->total_pages = totalPages,
page->total_items = n,
page->limit = this->_limitRows;
Expand Down
25 changes: 21 additions & 4 deletions phalcon/paginator/adapter/nativearray.zep
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,21 @@ class NativeArray extends Adapter

/**
* Returns a slice of the resultset to show in the pagination
*
* @deprecated will be removed after 4.0
*/
public function getPaginate() -> <\stdClass>
{
return this->paginate();
}

/**
* Returns a slice of the resultset to show in the pagination
*/
public function paginate() -> <\stdClass>
{
var config, items, page;
int show, pageNumber, totalPages, number, before, next;
int show, pageNumber, totalPages, number, previous, next;
double roundedTotal;

/**
Expand Down Expand Up @@ -118,18 +128,25 @@ class NativeArray extends Adapter
}

if pageNumber > 1 {
let before = pageNumber - 1;
let previous = pageNumber - 1;
} else {
let before = 1;
let previous = 1;
}

let page = new \stdClass(),
page->items = items,
page->first = 1,
page->before = before,
/**
* @deprecated `before` will be removed after 4.0
*/
page->before = previous,
page->previous = previous,
page->current = pageNumber,
page->last = totalPages,
page->next = next,
/**
* @deprecated `total_pages` will be removed after 4.0
*/
page->total_pages = totalPages,
page->total_items = number,
page->limit = this->_limitRows;
Expand Down
26 changes: 21 additions & 5 deletions phalcon/paginator/adapter/querybuilder.zep
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,21 @@ class QueryBuilder extends Adapter

/**
* Returns a slice of the resultset to show in the pagination
*
* @deprecated will be removed after 4.0
*/
public function getPaginate() -> <\stdClass>
{
return this->paginate();
}

/**
* Returns a slice of the resultset to show in the pagination
*/
public function paginate() -> <\stdClass>
{
var originalBuilder, builder, totalBuilder, totalPages,
limit, numberPage, number, query, page, before, items, totalQuery,
limit, numberPage, number, query, page, previous, items, totalQuery,
result, row, rowcount, next, sql, columns, db, hasHaving, hasGroup,
model, modelClass, dbService;

Expand Down Expand Up @@ -162,9 +172,9 @@ class QueryBuilder extends Adapter
let query = builder->getQuery();

if numberPage == 1 {
let before = 1;
let previous = 1;
} else {
let before = numberPage - 1;
let previous = numberPage - 1;
}

/**
Expand Down Expand Up @@ -253,15 +263,21 @@ class QueryBuilder extends Adapter
let page = new \stdClass(),
page->items = items,
page->first = 1,
page->before = before,
/**
* @deprecated `before` will be removed after 4.0
*/
page->before = previous,
page->previous = previous,
page->current = numberPage,
page->last = totalPages,
page->next = next,
/**
* @deprecated `total_pages` will be removed after 4.0
*/
page->total_pages = totalPages,
page->total_items = rowcount,
page->limit = this->_limitRows;

return page;
}

}
7 changes: 7 additions & 0 deletions phalcon/paginator/adapterinterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ interface AdapterInterface

/**
* Returns a slice of the resultset to show in the pagination
*
* @deprecated will be removed after 4.0
*/
public function getPaginate() -> <\stdClass>;

/**
* Returns a slice of the resultset to show in the pagination
*/
public function paginate() -> <\stdClass>;

/**
* Set current rows limit
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Paginator/Adapter/NativeArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,29 @@ function () {
expect($page)->isInstanceOf('stdClass');
expect($page->items)->count(25);

expect($page->previous)->equals(1);
expect($page->before)->equals(1);
expect($page->next)->equals(2);
expect($page->last)->equals(2);
expect($page->limit)->equals(25);

/**
* Now check by calling 'paginate()'
*/
$page = $paginator->paginate();

expect($page)->isInstanceOf('stdClass');
expect($page->items)->count(25);

expect($page->previous)->equals(1);
expect($page->before)->equals(1);
expect($page->next)->equals(2);
expect($page->last)->equals(2);
expect($page->limit)->equals(25);

expect($page->current)->equals(1);
expect($page->total_pages)->equals(2);

expect($page->current)->equals(1);
expect($page->total_pages)->equals(2);
}
Expand Down

0 comments on commit eb26399

Please sign in to comment.