Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to get resultset and total

How to achieve this query in phalcon using query builder (best method):

$results = " select * from table where name like 'A%' limit 10 ";  // <-- 10 records

$total = " select count(1) from table where name like 'A%' "; // <-- 100 records

return [
    'data' => $result,
    'total' => $total
];

im extjs user and i need to get total over limit to display paging information (eq: displaying 1 to 10 from 100 records)

thx.

are these methods valid?

$builder = new Builder();
$results = $builder
   ->from(__CLASS__)
   ->where('name like :name:', array('name'=>'A%'))
   ->andWhere(...)
   ->andWhere(...)
   ->orderby(...)
   ->limit(10)
   ->getQuery()
   ->execute();

$cloned = clone($builder);
$total = $cloned
    ->limit(null)
    ->getQuery()
    ->execute()
    ->count();


35.1k
Accepted
answer

I don't use query builder. But if it helps, here is a more simple approach:

        $itemsPerPage = 10;
        $currentPage = 1; // Implement for example $this->request->getQuery('page', 'int', 1)

        $result = YourTableTableModel::find(array(
            "name LIKE %:name:%",
            "bind" => array("name" => $name)
        ));

        $paginator = new \Phalcon\Paginator\Adapter\NativeArray(
            array(  
                "data" => $result,
                "limit"=> $itemsPerPage,
                "page" => $currentPage
            )
        );

        return $paginator->getPaginate();

helpful thanks