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

[BUG] Incorrect results from Phalcon\Paginator\Adapter\Model #1654

Closed
ghost opened this issue Dec 8, 2013 · 1 comment
Closed

[BUG] Incorrect results from Phalcon\Paginator\Adapter\Model #1654

ghost opened this issue Dec 8, 2013 · 1 comment

Comments

@ghost
Copy link

ghost commented Dec 8, 2013

Test case:

<?php

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' => 6,
        'page'  => 1,
        'data'  => new Test(),
);

$pager = new \Phalcon\Paginator\Adapter\Model($config);
for ($page=1; $page<=5; ++$page) {
        print_r($pager->setCurrentPage($page)->getPaginate());
}

When current page is 5 the output looks like this:

stdClass Object
(
    [items] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
            [4] => 1
            [5] => 1
        )

    [next] => 4
    [first] => 1
    [before] => 1
    [current] => 1
    [last] => 4
    [total_pages] => 4
    [total_items] => 20
)

What is wrong:

  • items must contain 0, not 1 (I must say it is a bit awkward that it is valid() method that is supposed to move forward the data pointer, not next())
  • if current is set to 1, why is next set to 4?
@ghost
Copy link
Author

ghost commented Dec 8, 2013

<?php

function dump_page($page)
{
        foreach ($page->items as $key => $item) {
                $page->items[$key] = $item->cedula;
        }

        print_r($page);
}

class Personnes extends Phalcon\Mvc\Model {}

$di = new Phalcon\DI();

$di->set('modelsManager', function() { return new Phalcon\Mvc\Model\Manager(); });
$di->set('modelsMetadata', function() { return new Phalcon\Mvc\Model\Metadata\Memory(); });
$di->set('db', function() { require 'unit-tests/config.db.php'; return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql); });

$personnes = Personnes::find(array('order' => 'cedula'));

$paginator = new Phalcon\Paginator\Adapter\Model(array(
        'data' => $personnes,
        'limit' => 10,
        'page' => 1
));

dump_page($paginator->setCurrentPage(1)->getPaginate());
dump_page($paginator->setCurrentPage(218)->getPaginate());
dump_page($paginator->setCurrentPage(219)->getPaginate());
dump_page($paginator->setCurrentPage(220)->getPaginate());

Output:

stdClass Object
(
    [items] => Array
        (
            [0] => 1
            [1] => 100
            [2] => 1003
            [3] => 1009
            [4] => 101
            [5] => 1011
            [6] => 1020
            [7] => 1025
            [8] => 1034
            [9] => 104
        )

    [next] => 2
    [first] => 1
    [before] => 1
    [current] => 1
    [last] => 218
    [total_pages] => 218
    [total_items] => 2180
)
stdClass Object
(
    [items] => Array
        (
            [0] => T-Cx205
            [1] => T-Cx206
            [2] => T-Cx207
            [3] => T-Cx208
            [4] => T-Cx209
            [5] => T-Cx210
            [6] => T-Cx211
            [7] => T-Cx212
            [8] => T-Cx213
            [9] => T-Cx214
        )

    [next] => 218
    [first] => 1
    [before] => 217
    [current] => 218
    [last] => 218
    [total_pages] => 218
    [total_items] => 2180
)
stdClass Object
(
    [items] => Array
        (
        )

    [next] => 218
    [first] => 1
    [before] => 218
    [current] => 219
    [last] => 218
    [total_pages] => 218
    [total_items] => 2180
)
stdClass Object
(
    [items] => Array
        (
            [0] => 100
            [1] => 1003
            [2] => 1009
            [3] => 101
            [4] => 1011
            [5] => 1020
            [6] => 1025
            [7] => 1034
            [8] => 104
            [9] => 1046
        )

    [next] => 218
    [first] => 1
    [before] => 1
    [current] => 1
    [last] => 218
    [total_pages] => 218
    [total_items] => 2180
)

Page 219 does not return anything, page 220 misses the very first element.

@ghost ghost mentioned this issue Dec 8, 2013
phalcon pushed a commit that referenced this issue Dec 9, 2013
@ghost ghost mentioned this issue Dec 10, 2013
phalcon pushed a commit that referenced this issue Dec 10, 2013
@phalcon phalcon closed this as completed Dec 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant