Skip to content

Commit

Permalink
Merge pull request #1659 from sjinks/1.2.5
Browse files Browse the repository at this point in the history
[1.2.5] Fix #1654
  • Loading branch information
Phalcon committed Dec 10, 2013
2 parents 727e876 + 8119125 commit d0a40dd
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Make sure that 'persistent' is resolved only when accessed for the first time (#1637)
- Fix Phalcon\Session\Bag::remove() (#1637)
- Bug fixes in beanstalkd protocol implementation
- Phalcon\Paginator\Adapter\Model returns correct results even when page number is incorrect (#1654)

1.2.4
- Fixed broken ACL inheritance (#905)
Expand Down
8 changes: 5 additions & 3 deletions build/32bits/phalcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -96669,7 +96669,7 @@ static PHP_METHOD(Phalcon_Paginator_Adapter_Model, setCurrentPage){
phalcon_fetch_params(0, 1, 0, &page);

phalcon_update_property_this_quick(this_ptr, SL("_page"), page, 3986343137UL TSRMLS_CC);

RETURN_THISW();
}

static PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){
Expand Down Expand Up @@ -96739,11 +96739,13 @@ static PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){
array_init(page_items);
if (PHALCON_GT(n, zero)) {

if (PHALCON_LE(start, n)) {
if (PHALCON_LT(start, n)) {
phalcon_call_method_p1_key(NULL, items, "seek", start, 274276301UL);
} else {
phalcon_call_method_p1_key(NULL, items, "seek", one, 274276301UL);
phalcon_call_method_p1_key(NULL, items, "seek", zero, 274276301UL);
PHALCON_CPY_WRT(page_number, one);
PHALCON_CPY_WRT(last_show_page, zero);
PHALCON_CPY_WRT(start, zero);
}

PHALCON_INIT_VAR(i);
Expand Down
8 changes: 5 additions & 3 deletions build/64bits/phalcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -96669,7 +96669,7 @@ static PHP_METHOD(Phalcon_Paginator_Adapter_Model, setCurrentPage){
phalcon_fetch_params(0, 1, 0, &page);

phalcon_update_property_this_quick(this_ptr, SL("_page"), page, 6953243428065UL TSRMLS_CC);

RETURN_THISW();
}

static PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){
Expand Down Expand Up @@ -96739,11 +96739,13 @@ static PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){
array_init(page_items);
if (PHALCON_GT(n, zero)) {

if (PHALCON_LE(start, n)) {
if (PHALCON_LT(start, n)) {
phalcon_call_method_p1_key(NULL, items, "seek", start, 210727673805UL);
} else {
phalcon_call_method_p1_key(NULL, items, "seek", one, 210727673805UL);
phalcon_call_method_p1_key(NULL, items, "seek", zero, 210727673805UL);
PHALCON_CPY_WRT(page_number, one);
PHALCON_CPY_WRT(last_show_page, zero);
PHALCON_CPY_WRT(start, zero);
}

PHALCON_INIT_VAR(i);
Expand Down
8 changes: 5 additions & 3 deletions build/safe/phalcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -96669,7 +96669,7 @@ static PHP_METHOD(Phalcon_Paginator_Adapter_Model, setCurrentPage){
phalcon_fetch_params(0, 1, 0, &page);

phalcon_update_property_this(this_ptr, SL("_page"), page TSRMLS_CC);

RETURN_THISW();
}

static PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){
Expand Down Expand Up @@ -96739,11 +96739,13 @@ static PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){
array_init(page_items);
if (PHALCON_GT(n, zero)) {

if (PHALCON_LE(start, n)) {
if (PHALCON_LT(start, n)) {
phalcon_call_method_p1_noret(items, "seek", start);
} else {
phalcon_call_method_p1_noret(items, "seek", one);
phalcon_call_method_p1_noret(items, "seek", zero);
PHALCON_CPY_WRT(page_number, one);
PHALCON_CPY_WRT(last_show_page, zero);
PHALCON_CPY_WRT(start, zero);
}

PHALCON_INIT_VAR(i);
Expand Down
8 changes: 5 additions & 3 deletions ext/paginator/adapter/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ PHP_METHOD(Phalcon_Paginator_Adapter_Model, setCurrentPage){
phalcon_fetch_params(0, 1, 0, &page);

phalcon_update_property_this(this_ptr, SL("_page"), page TSRMLS_CC);

RETURN_THISW();
}

/**
Expand Down Expand Up @@ -180,11 +180,13 @@ PHP_METHOD(Phalcon_Paginator_Adapter_Model, getPaginate){
/**
* Seek to the desired position
*/
if (PHALCON_LE(start, n)) {
if (PHALCON_LT(start, n)) {
phalcon_call_method_p1_noret(items, "seek", start);
} else {
phalcon_call_method_p1_noret(items, "seek", one);
phalcon_call_method_p1_noret(items, "seek", zero);
PHALCON_CPY_WRT(page_number, one);
PHALCON_CPY_WRT(last_show_page, zero);
PHALCON_CPY_WRT(start, zero);
}

/**
Expand Down
136 changes: 136 additions & 0 deletions ext/tests/issue-1654.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
--TEST--
Incorrect results from Phalcon\Paginator\Adapter\Model - https://github.com/phalcon/cphalcon/issues/1654
--SKIPIF--
<?php include('skipif.inc'); ?>
--FILE--
<?php

function dump_page($page)
{
$page->items = $page->items[0]; // Because Resultsets are weird and advance the current position in valid() not in next()
print_r($page);
}

class Test implements SeekableIterator, Countable
{
private $data = array(
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
);

private $pos = 0;

public function seek($pos)
{
$this->pos = $pos;
}

public function rewind()
{
$this->pos = 0;
}

public function current()
{
return $this->data[$this->pos];
}

public function key()
{
return $this->pos;
}

public function next()
{
++$this->pos;
}

public function valid()
{
return isset($this->data[$this->pos]);
}

public function count()
{
return count($this->data);
}
}

$config = array(
'limit' => 5,
'page' => 1,
'data' => new Test(),
);

$pager = new \Phalcon\Paginator\Adapter\Model($config);
for ($page=1; $page<=6; ++$page) {
dump_page($pager->setCurrentPage($page)->getPaginate());
}
?>
--EXPECT--
stdClass Object
(
[items] => 0
[next] => 2
[first] => 1
[before] => 1
[current] => 1
[last] => 4
[total_pages] => 4
[total_items] => 20
)
stdClass Object
(
[items] => 5
[next] => 3
[first] => 1
[before] => 1
[current] => 2
[last] => 4
[total_pages] => 4
[total_items] => 20
)
stdClass Object
(
[items] => 10
[next] => 4
[first] => 1
[before] => 2
[current] => 3
[last] => 4
[total_pages] => 4
[total_items] => 20
)
stdClass Object
(
[items] => 15
[next] => 4
[first] => 1
[before] => 3
[current] => 4
[last] => 4
[total_pages] => 4
[total_items] => 20
)
stdClass Object
(
[items] => 0
[next] => 2
[first] => 1
[before] => 1
[current] => 1
[last] => 4
[total_pages] => 4
[total_items] => 20
)
stdClass Object
(
[items] => 0
[next] => 2
[first] => 1
[before] => 1
[current] => 1
[last] => 4
[total_pages] => 4
[total_items] => 20
)

0 comments on commit d0a40dd

Please sign in to comment.