dudapiotr / ZfTable

*Not supported*ZfTable 3.1. Awesome ZF2 table / grid (and much much more) generator with huge possibilities of decorating and conditioning. Integrated with DataTables, Doctrine 2, Bootstrap 2.0 and 3.0.
http://dudapiotrek.laohost.net/
MIT License
75 stars 59 forks source link

Problem with Doctrine pagination #76

Open cbichis opened 9 years ago

cbichis commented 9 years ago

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

W33k3nd commented 9 years ago

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');
cbichis commented 9 years ago

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

W33k3nd commented 9 years ago

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 commented 9 years ago

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

cbichis commented 9 years ago

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 commented 9 years ago

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

aNorthernSoul commented 7 years ago

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.