In this post we are going to go over the required steps to create a Doctrine adapter for Zend\Paginator.
Step 1: Creating the adapter
What a surprise. Assuming the you are working on the Application Module create a file called Adapter.php on module/Application/src/Application/Paginator
<?php
namespace Application\Paginator;
use Zend\Paginator\Adapter\AdapterInterface;
class Adapter implements AdapterInterface
{
protected $repository;
/**
* Construct
*
* @param \Doctrine\ORM\EntityRepository $repository Repository class
*/
public function __construct($repository)
{
$this->repository = $repository;
}
/**
* Returns an collection of items for a page.
*
* @param int $offset Page offset
* @param int $itemCountPerPage Number of items per page
*
* @return array
*/
public function getItems($offset, $itemCountPerPage)
{
return $this->repository->getItems($offset, $itemCountPerPage);
}
/**
* Count results
*
* @return int
*/
public function count()
{
return $this->repository->count();
}
}
Our adapter implements the Zend\Paginator\Adapter\AdapterInterface interface, so we need to implement the methods getItems() and count()
Notice the we are injecting the repository class in the adapter’s contructor. We are going to take care of this dependency in the next steps.
Step 2: Using Service Manager to create the Paginator Service
On ‘module/Application/Module.php’ we are going to use the getServiceConfig() to setup our Paginator Service and injecting the required dependencies. It looks like this:
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Application\Entity\Repository\User")
* @ORM\Table(name="user")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=200)
*/
protected $name;
/**
* @ORM\Column(type="string", length=200)
*/
protected $email;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @return mixed
*/
public function getName()
{
return $this->name;
}
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return mixed
*/
public function getEmail()
{
return $this->email;
}
}
Observe this line: @ORM\Entity(repositoryClass="Application\Entity\Repository\User"). It will be in this class that we are going to implements the methods used by our custom adapter.
<?php
namespace Application\Entity\Repository;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
class User extends EntityRepository
{
/**
* Counts how many users there are in the database
*
* @return int
*/
public function count()
{
$query = $this->getEntityManager()->createQueryBuilder();
$query->select(array('u.id'))
->from('Application\Entity\User', 'u');
$result = $query->getQuery()->getResult();
return count($result);
}
/**
* Returns a list of users
*
* @param int $offset Offset
* @param int $itemCountPerPage Max results
*
* @return array
*/
public function getItems($offset, $itemCountPerPage)
{
$query = $this->getEntityManager()->createQueryBuilder();
$query->select(array('u.id', 'u.name', 'u.email'))
->from('Application\Entity\User', 'u')
->setFirstResult($offset)
->setMaxResults($itemCountPerPage);
$result = $query->getQuery()->getResult(Query::HYDRATE_ARRAY);
return $result;
}
}
We now have all set to start using the adapter.
Step 4: Using the paginator adapter on your controller
Using our adapter is pretty straight foward. All we need to do is call the service and pass it to the views,
123456789101112131415161718192021
<?php
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$users = $this->getServiceLocator()->get('Application\Paginator\User');
return new ViewModel(
array(
'users' => $users
)
);
}
}