ZfTable
ZfTable copied to clipboard
Problem with Doctrine pagination
Hi,
It seems we are affected by the same problem as
http://www.doctrine-project.org/jira/browse/DDC-1927
By example for this source:
protected function getSource() {
$queryBuilder = $this->getObjectManager()->createQueryBuilder();
$queryBuilder
->select('e.id id')
->from('Application\Entity\Package', 'e')
->join('e.user', 'u');
return $queryBuilder;
}
Is returned an error:
Not all identifier properties can be found in the ResultSetMapping: id
I am trying to select multiple fields from each table, of course, I reduced the sample to minimum to see the problem...
I 've got the same problem. I think this problem happens, because Doctrine Paginator missed the join identifier.
$queryBuilder
->select('e', 'u')
->from('Application\Entity\Package', 'e')
->join('e.user', 'u');
Is that working for you ?
I don't think I can mass select the u entity, I need to only fetch from second table a CONCAT(u.first_name, ' ', u.last_name) staff_name
No! My other problem was, i used two id annotations in one of my tables (Id-> filename, Id -> group). Doctrine Paginator can't handle this yet. http://www.doctrine-project.org/jira/browse/DDC-2213
Also i solved my problem to add an auto increment id and canceled the other id's as normal values and add an unique index with filename and group.
Perhaps help this https://github.com/doctrine/DoctrineORMModule/issues/66
I found a workaround.
ZfTable\Source\DoctrineQueryBuilder getPaginator() should be modified as below:
public function getPaginator()
{
if (!$this->paginator) {
$this->order();
$doctrinePaginator = new ORMPaginator($this->query);
$doctrinePaginator->setUseOutputWalkers(false);
$adapter = new DoctrineAdapter($doctrinePaginator);
$this->paginator = new Paginator($adapter);
$this->initPaginator();
}
return $this->paginator;
}
I think we should have a out of the box way to disable the OutputWalker.
I ran into the issue of the OutputWalkers for optimization and did the following.
In my table classes that extend AbstractTable I added the following function:
` use Application\Helpers\Extensions\ExtendZfTableDoctrineQueryBuilder;
class MyTable extends AbstractTable {
...
public function setExtendedDoctrineSource($source)
{
if ($source instanceof QueryBuilder) {
$source = new ExtendZfTableDoctrineQueryBuilder($source);
} else {
throw new \Exception('This type of source is undefined');
}
$source->setTable($this);
$this->source = $source;
return $this;
}
} `
Then I have the following as ExtendZfTableDoctrineQueryBuilder which allows me to configure Doctrine as I wanted.
` namespace Application\Helpers\Extensions;
use ZfTable\Source\DoctrineQueryBuilder; use ZfTable\Source\AbstractSource; use Zend\Paginator\Paginator; use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter; use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
class ExtendZfTableDoctrineQueryBuilder extends DoctrineQueryBuilder {
/**
*
* @var \Doctrine\ORM\QueryBuilder
*/
protected $query;
/**
*
* @var \Zend\Paginator\Paginator
*/
protected $paginator;
/**
*
* @param \Doctrine\ORM\QueryBuilder $query
*/
public function __construct($query)
{
$this->query = $query;
}
/**
*
* @return \Zend\Paginator\Paginator
*/
public function getPaginator()
{
if (!$this->paginator) {
$this->order();
$ormPaginator = new ORMPaginator($this->query);
$ormPaginator->setUseOutputWalkers(false);
$adapter = new DoctrineAdapter($ormPaginator);
$this->paginator = new Paginator($adapter);
$this->initPaginator();
}
return $this->paginator;
}
protected function order()
{
$column = $this->getParamAdapter()->getColumn();
$order = $this->getParamAdapter()->getOrder();
if (!$column) {
return;
}
$header = $this->getTable()->getHeader($column);
$tableAlias = ($header) ? $header->getTableAlias() : 'q';
if (false === strpos($tableAlias, '.')) {
$tableAlias = $tableAlias.'.'.$column;
}
$this->query->orderBy($tableAlias, $order);
}
public function getQuery()
{
return $this->query;
}
} `
Sorry, hopefully that is readable. I can't seem to keep my code within the code blocks.