I'm using paginator first time, and now I have some questions how to and where to do things.
Here my ideas and underneath some code.
I understood to object paginator in my controller action, give the object to my datacollection an give this to my ViewModel Object.
Because I get an error about the ArrayAdapter I will post also the error and my use statements in my controller class.
error:
Class 'Stammdaten\Controller\ArrayAdapter' not found
xyzController.php
use Zend\Paginator\Adapter\AdapterInterface;
use Zend\Paginator\Paginator;
use Zend\Paginator\ScrollingStyle\Sliding;
public function indexAction()
{
$page = $this->params()->fromRoute('page');
$paginator= new Paginator(new ArrayAdapter($this->mandantTable->Ansprechpartnerjoin()->toArray()));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage(20);
$paginator->setPageRange(5);
return new ViewModel([
'paginator' => $paginator,
]);
}
First question here: Is this adapter the right one, because I give a tableadapter collection or do I have to change to another adapter?
I modified my routing for using paginator like follows:
module.config.php
'router' => [
'routes' => [
'mandant' => [
'type' => Segment::class,
'options' => [
'route' => '/mandant[/:action[/:id[/:page]]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9\-]*',
],
'defaults' => [
'controller' => Controller\MandantController::class,
'action' => 'index',
],
],
],
'paginator' => [
'type' => 'segment',
'options' => [
'route' => '/list/[page/:page]',
'defaults' => [
'page' => 1,
],
],
],
],
],
Next question here: is it needed to modify the route for MandantController as well or would it be enough to have the paginator route?
And here part of my viewscript, index.phtml:
foreach ($this->paginator->getCurrentItems() as $mandant) :?>
<tr>
<td><?= $this->escapeHtml($mandant['somefield']) ?></td>
</tr>
<?php endforeach;
echo $this->paginator, 'Sliding', 'pagination/control', array('route' => 'mandant')
?>
Next Question here, I gave the paginator object to an array, because of the arrayadapter, but I really would prefer to have an object rather than an array, is this possible or do I have to use another adapter?
And last question: Is everything done or am I missing something somewhere?
EDIT 1: I tried a bit further, and have now changed to the DbSelect Adapter, but it seems like my tableadapter won't be accepted. This is what I changed now:
xyzController.php
$page = $this->params()->fromRoute('page');
$countQuery = new Select();
$countQuery
->from('t_mandant')
->columns([ DbSelect::ROW_COUNT_COLUMN_NAME => 'id' ]);
$paginator= new Paginator(new DbSelect($this->mandantTable->Ansprechpartnerjoin(),null, $countQuery));
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage(20);
$paginator->setPageRange(5);
return new ViewModel([
'paginator' => $paginator,
]);
The error message:
Argument 1 passed to Zend\Paginator\Adapter\DbSelect::__construct() must be an instance of Zend\Db\Sql\Select, instance of Zend\Db\ResultSet\ResultSet given
This is of course right, my question is, what do I have to do, to give the tableAdapter instance to the paginator constructor.
Now I changed to another solution. I construct the paginator in my model.
It now looks like follows:
$resultSetprototype=new ResultSet();
$resultSetprototype->setArrayObjectPrototype(new Mandant());
$paginatorAdapter= new DbSelect($select, $this->tableGateway->getAdapter(), $resultSetprototype);
$paginator=new Paginator($paginatorAdapter);
return $paginator;
Now it works quite well.