Phalcon\Paginator\Adapter\QueryBuilder::getPaginate(): Trying to get property of non-object

Hello

When I use Phalcon\Paginator\Adapter\QueryBuilder::getPaginate();

ie

$paginator=new Phalcon\Paginator\Adapter\QueryBuilder(
    array(
        "builder" => $items,
        "limit" => 10,
        "page" => $this->request->Get('page')
    )
);

I get the following error when no results can be found.

Phalcon\Paginator\Adapter\QueryBuilder::getPaginate(): Trying to get property of non-object

Is there a fix for this?

When no results can be found - what does$items look like? If $items is empty, that could cause that type of error.

edited May '14

$items is a query from $this->modelsManager->createBuilder()

I have now:

        $query=$this->modelsManager->createBuilder()
            ->from('Articles')
            ->innerJoin('Categoryarticles','Articles.id=Categoryarticles.articleid')
            ->innerJoin('Categories','Categoryarticles.categoryid=Categories.id')
            ->where('Articles.languagekey=:languagekey:',array('languagekey' => $this->languagekey()))
            ->andWhere('Articles.trash=:trash:',array('trash' => 'no'))
            ->andWhere('Articles.publishdate<=:date:',array('date' => date('Y-m-d')))
            ->andWhere('Articles.expiredate>:date:',array('date' => date('Y-m-d')))
            ->andWhere('Categories.trash=:trash:',array('trash' => 'no'))
            ->andWhere('Categories.languagekey=:languagekey:',array('languagekey' => $this->languagekey()));

If I have at least one result, no errors occured. If no results, I get the error. The query mentioned here works fine.

Using

$paginator=new Phalcon\Paginator\Adapter\Model(
    array(
        "data" => $items,
        "limit" => 10,
        "page" => $this->request->Get('page')
    )
);

Does not give any problems, but honestly I don't like the solution with Phalcon\Paginator\Adapter\Model over more than 10000 records.

I think Phalcon should handle the situation of an empty data-set better. It might be worth it to submit a bug report.

In the meantime, can't you just check the size of $items before sending it to the Paginator?

Hello,

For now I have created an empty object with the same properties as the Paginator, but I've expected an empty Paginate object as in Phalcon\Paginator\Adapter\Model when my data is empty.

ie:

                $object=new stdClass(); 
                $object->items=array();
                $object->total_pages=0;
                $object->total_items=0;
                return $object;

It's not the best solution, but it avoids hours of rewriting my views.

I have submitted a bug report



6.2k

I had the same issue, how did you check you data is empty? do you have to SELECT count with additional query?

Hello,

For now I have created an empty object with the same properties as the Paginator, but I've expected an empty Paginate object as in Phalcon\Paginator\Adapter\Model when my data is empty.

ie:

``` $object=new stdClass(); $object->items=array(); $object->totalpages=0; $object->totalitems=0; return $object;

```

It's not the best solution, but it avoids hours of rewriting my views.

I have submitted a bug report