0

In my application I want to make use of the paginator of Zend Framework 2. The problem I faced is that I get an object result set from my table instead of a array.

I can simple fix this by looping through the object with a foreach and generate a new array. But In my eyes, it's a workaround and not necessary at al to do it like this.

The code I've got for my table:

VideosTable.php

namespace Application\Model;

use Zend\Db\Sql\Expression;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Application\Model\Videos;

class VideosTable extends AbstractTableGateway
{
    protected $table = 'videos';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Videos());

        $this->initialize();
    }

    /**
     * Get movie list
     */
    public function getMovies($limit, $order, $array = null)
    {
        $select = new Select();
        $select->from($this->table);
        $select->order('id', $order)->limit($limit);

        $resultSet = $this->selectWith($select);

        return $resultSet;
    }
}

The paginator I've set in my controller:

public function indexAction()
    {   
        $select    = new Select();
        $page      = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1;
        $videos    = $this->getVideosTable()->getMovies(25, 'DESC');

        // could do like this but looks not necessary / dirty
        foreach ($videos as $key => $video) :
            $data[] = array('video_title' => $video->video_title);          
        endforeach;


        $paginator = new Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($data));
        $paginator->setCurrentPageNumber($page)
                  ->setItemCountPerPage(11)
                  ->setPageRange(7);
etc..

Thanks in advance,

Nick

directory
  • 3,093
  • 8
  • 45
  • 85
  • Maybe take a look at the answer here -> http://stackoverflow.com/questions/12441529/zend-framework-2-paginator-tablegateway – Crisp Sep 12 '13 at 18:04

1 Answers1

1

Change your getMovies method to return something like this

/**
 * Get movie list
 */
public function getMovies($limit, $order, $array = null)
{
    $select = new Select();
    $select->from($this->table);
    $select->order('id', $order)->limit($limit);

    return new \Zend\Paginator\Paginator(
        new \Zend\Paginator\Adapter\DbSelect($select, $this->adapter, $this->resultSetPrototype)
    );
}

Then you'll can do as you please with that paginator in your controller. It will also allow you to ditch that limit logic from the getMovies() method as it will be handled by the paginator.

Ditching the limit logic (as it's handled by the paginator) would look something like this

/**
 * Get movie list
 */
public function getMovies($order = 'DESC')
{
    $select = $this->getSql()->select();
    $select->order('id', $order);

    return new \Zend\Paginator\Paginator(
        new \Zend\Paginator\Adapter\DbSelect($select, $this->adapter, $this->resultSetPrototype)
    );
}
Tim Klever
  • 601
  • 5
  • 13