ZfTable icon indicating copy to clipboard operation
ZfTable copied to clipboard

Problem with Doctrine pagination

Open cbichis opened this issue 9 years ago • 7 comments

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...

cbichis avatar Sep 03 '15 07:09 cbichis

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');

W33k3nd avatar Sep 03 '15 08:09 W33k3nd

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

cbichis avatar Sep 03 '15 08:09 cbichis

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.

W33k3nd avatar Sep 03 '15 09:09 W33k3nd

Perhaps help this https://github.com/doctrine/DoctrineORMModule/issues/66

W33k3nd avatar Sep 03 '15 09:09 W33k3nd

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;
    }

cbichis avatar Sep 03 '15 13:09 cbichis

I think we should have a out of the box way to disable the OutputWalker.

cbichis avatar Sep 03 '15 17:09 cbichis

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.

aNorthernSoul avatar Mar 30 '17 13:03 aNorthernSoul