We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

items empty in getPaginate()

When attempting to present data with a filter on the data. Items are empty when displayed on view:

This is how im getting the result set:

$vehicles = Vehicles::find(array($vehicleWhere));

$paginator = new Paginator(array(
            "data" => $vehicles,
            "limit" => 10,
            "page" => $numberPage
        ));

This is a vardump of the $vehicles, $numberPage, and $page.

object(stdClass)[90]
  public 'items' => 
    array (size=0)
      empty
  public 'next' => int 1
  public 'first' => int 1
  public 'before' => int 1
  public 'current' => int 1
  public 'last' => int 1
  public 'total_pages' => int 1
  public 'total_items' => int 5
int 1
object(Phalcon\Mvc\Model\Resultset\Simple)[84]
  protected '_type' => int 0
  protected '_result' => 
    object(Phalcon\Db\Result\Pdo)[85]
      protected '_connection' => 
        object(Phalcon\Db\Adapter\Pdo\Mysql)[79]
          protected '_eventsManager' => 
            object(Phalcon\Events\Manager)[76]
              ...
          protected '_descriptor' => 
            array (size=4)
              ...
          protected '_dialect' => 
            object(Phalcon\Db\Dialect\Mysql)[81]
              ...
          protected '_connectionId' => int 1
          protected '_sqlStatement' => string 'SELECT `vehicles`.`Year` AS `Year` FROM `vehicles` GROUP BY `vehicles`.`Year` ORDER BY `vehicles`.`Year`' (length=104)
          protected '_sqlVariables' => null
          protected '_sqlBindTypes' => null
          protected '_transactionsWithSavepoints' => int 0
          protected '_pdo' => 
            object(PDO)[80]
              ...
          protected '_affectedRows' => null
          protected '_transactionLevel' => int 0
          protected '_type' => string 'mysql' (length=5)
          protected '_dialectType' => string 'mysql' (length=5)
      protected '_result' => null
      protected '_fetchMode' => int 2
      protected '_pdoStatement' => 
        object(PDOStatement)[86]
          public 'queryString' => string 'SELECT `vehicles`.`Id`, `vehicles`.`Vehicle_Makes_Id`, `vehicles`.`Vehicle_Models_Id`, `vehicles`.`Year` FROM `vehicles` WHERE `vehicles`.`Year` = 1998 AND `vehicles`.`Vehicle_Makes_Id` = 5' (length=189)
      protected '_sqlStatement' => string 'SELECT `vehicles`.`Id`, `vehicles`.`Vehicle_Makes_Id`, `vehicles`.`Vehicle_Models_Id`, `vehicles`.`Year` FROM `vehicles` WHERE `vehicles`.`Year` = 1998 AND `vehicles`.`Vehicle_Makes_Id` = 5' (length=189)
      protected '_bindParams' => null
      protected '_bindTypes' => null
      protected '_rowCount' => int 5
  protected '_cache' => null
  protected '_isFresh' => boolean true
  protected '_pointer' => int 0
  protected '_count' => int 5
  protected '_activeRow' => boolean false
  protected '_rows' => 
    array (size=5)
      0 => 
        array (size=4)
          'Id' => string '1065' (length=4)
          'Vehicle_Makes_Id' => string '5' (length=1)
          'Vehicle_Models_Id' => string '27' (length=2)
          'Year' => string '1998' (length=4)
      1 => 
        array (size=4)
          'Id' => string '1066' (length=4)
          'Vehicle_Makes_Id' => string '5' (length=1)
          'Vehicle_Models_Id' => string '28' (length=2)
          'Year' => string '1998' (length=4)
      2 => 
        array (size=4)
          'Id' => string '1067' (length=4)
          'Vehicle_Makes_Id' => string '5' (length=1)
          'Vehicle_Models_Id' => string '30' (length=2)
          'Year' => string '1998' (length=4)
      3 => 
        array (size=4)
          'Id' => string '1068' (length=4)
          'Vehicle_Makes_Id' => string '5' (length=1)
          'Vehicle_Models_Id' => string '31' (length=2)
          'Year' => string '1998' (length=4)
      4 => 
        array (size=4)
          'Id' => string '1069' (length=4)
          'Vehicle_Makes_Id' => string '5' (length=1)
          'Vehicle_Models_Id' => string '585' (length=3)
          'Year' => string '1998' (length=4)
  protected '_errorMessages' => null
  protected '_hydrateMode' => int 0
  protected '_model' => 
    object(WP\Models\Vehicles)[72]
      public 'Id' => null
      public 'Vehicle_Makes_Id' => null
      public 'Vehicle_Models_Id' => null
      public 'Year' => null
      protected '_dependencyInjector' =>        
        object(Phalcon\DI\FactoryDefault)[5]
          public '_services' => 
            array (size=27)
              ...
          public '_sharedInstances' => 
            array (size=15)
              ...
          public '_freshInstance' => boolean true
      protected '_modelsManager' => 
        object(Phalcon\Mvc\Model\Manager)[70]
          protected '_dependencyInjector' => 
            object(Phalcon\DI\FactoryDefault)[5]
              ...
          protected '_eventsManager' => null
          protected '_customEventsManager' => null
          protected '_readConnectionServices' => null
          protected '_writeConnectionServices' => null
          protected '_aliases' => 
            array (size=2)
              ...
          protected '_hasMany' => null
          protected '_hasManySingle' => null
          protected '_hasOne' => null
          protected '_hasOneSingle' => null
          protected '_belongsTo' => 
            array (size=2)
              ...
          protected '_belongsToSingle' => 
            array (size=1)
              ...
          protected '_hasManyToMany' => null
          protected '_hasManyToManySingle' => null
          protected '_initialized' => 
            array (size=1)
              ...
          protected '_sources' => 
            array (size=1)
              ...
          protected '_schemas' => null
          protected '_behaviors' => null
          protected '_lastInitialized' => 
            &object(WP\Models\Vehicles)[72]
          protected '_lastQuery' => null
          protected '_reusable' => null
          protected '_keepSnapshots' => null
          protected '_dynamicUpdate' => null
          protected '_namespaceAliases' => null
      protected '_modelsMetaData' => null
      protected '_errorMessages' => null
      protected '_operationMade' => int 0
      protected '_dirtyState' => int 1
      protected '_transaction' => null
      protected '_uniqueKey' => null
      protected '_uniqueParams' => null
      protected '_uniqueTypes' => null
      protected '_skipped' => null
      protected '_related' => null
      protected '_snapshot' => null
  protected '_columnMap' => null
  protected '_keepSnapshots' => boolean false         

Which adapter you use as Paginator class? If you want to use resultset as data, then you should use \Phalcon\Paginator\Adapter\Model

More info: https://docs.phalcon.io/en/latest/reference/pagination.html#adapters-usage



2.5k

That is the Adapter I am using:

use Phalcon\Paginator\Adapter\Model as Paginator;

Hey,

sorry for injecting to your topic.

I've the same problem. Did you solved it? May you can share your experience with me?

Thanks a lot OP =)

edited Sep '14

Try using the Builder, see below,

Here is what I do:


    \Phalcon\Mvc\Model\Transaction\Manager as TransactionManager;

    /**
     * @return void
     */
    public function indexAction()
    {
        $page = (int) $this->request->get("page");
        $page = (!$page) ? 1 : (int) $page;

        $builder = $this->modelsManager->createBuilder()
            ->columns('id, user_id, transaction_id, type, gateway, amount, created_at')
            ->from('Transaction') // Notice the capitalized model name
            ->orderBy('id');

        $paginator = new \Phalcon\Paginator\Adapter\QueryBuilder([
            "builder" => $builder,
            "limit"=> 50,
            "page" => $page
        ]);

        // Get the paginated results
        $page = $paginator->getPaginate();

        $this->view->setVars([
            'page' => $page,
        ]);
        $this->view->pick('admin/transaction');
    }
    {% for transaction in page.items %}
    ...
    <tr>
        <td>{{ transaction.id }}</td>
        <td>{{ transaction.user_id }}</td>
        <td>{{ transaction.transaction_id }}</td>
        <td>{{ transaction.type }}</td>
        <td>{{ transaction.gateway }}</td>
        <td>{{ transaction.amount }}</td>
        <td>{{ transaction.amount_after_discount }}</td>
        <td>{{ transaction.created_at }}</td>
    </tr>
    ...
    {% endfor %}

<ul class="pagination pagination-large">
       <l}><a href="{{ url('admin/transaction') }}"><span class="glyphicon glyphicon-fast-backward"></span></a></li>
       <li><a href="{{ url('admin/transaction') }}?page=<?= $page->before; ?>"><span class="glyphicon glyphicon-chevron-left"></span></a></li>
       <li><a href="{{ url('admin/transaction') }}?page=<?= $page->next; ?>"><span class="glyphicon glyphicon-chevron-right"></span></a></li>
       <li><a href="{{ url('admin/transaction') }}?page=<?= $page->last; ?>"><span class="glyphicon glyphicon-fast-forward"></span></a></li>
</ul>

This is fragment from my grid helper.


    public function setData(Builder $data)
    {
        $this->_data = $data;
        return $this;
    }

    public function getData()
    {
        return $this->_data;
    }

    public function loadData()
    {
        if (is_null($this->_paginator))
        {
            $currentPage = ($this->request->has('page')) ? (int)$this->request->get('page') : 1;

            $data = clone $this->getData();
            $data->columns(sprintf('count(%s.id) as total', $this->getName()));
            $total = $data->getQuery()->execute()->getFirst()->total;

            if ($currentPage > ceil($total / $this->getLimit()) || $currentPage < 1)
                $currentPage = 1;

            $paginator = new \Phalcon\Paginator\Adapter\QueryBuilder(
                array(
                    "builder" => $this->getData(),
                    "limit"=> $this->getLimit(),
                    "page" => $currentPage
                )
            );

            $this->_paginator = $paginator->getPaginate();
        }

        return $this->_paginator;
    }

    public function build()
    {
        return $this->viewSimple->render('blocks/grid/layout', array('grid' => $this, 'paginator' => $this->loadData()));
    }

Solve this problem. If you use code from base example:

$currentPage = (int) $_GET["page"];

The only problem is value of $currentPage. it will turn 0 for very first page (if $_GET['page'] is not defined). Thats why you receive empty array in $paginator->items

Use this construction:

$currentPage = ($this->request->has('page')) ? (int)$this->request->get('page') : 1;