Completed
Pull Request — 3.x (#844)
by Marko
01:58
created

Pager::appendTreeWalker()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineORMAdminBundle\Datagrid;
13
14
use Doctrine\ORM\Query;
15
use Sonata\AdminBundle\Datagrid\Pager as BasePager;
16
17
/**
18
 * Doctrine pager class.
19
 *
20
 * @author Jonathan H. Wage <[email protected]>
21
 */
22
class Pager extends BasePager
23
{
24
    /**
25
     * NEXT_MAJOR: remove this property.
26
     *
27
     * @deprecated This property is deprecated since version 2.4 and will be removed in 3.0
28
     */
29
    protected $queryBuilder = null;
30
31
    public function computeNbResult()
32
    {
33
        $countQuery = clone $this->getQuery();
34
35
        if (\count($this->getParameters()) > 0) {
36
            $countQuery->setParameters($this->getParameters());
37
        }
38
        
39
        $countQuery->select(sprintf(
40
            "count(%s %s) as cnt",
41
            $countQuery instanceof ProxyQuery && !$countQuery->isDistinct() ? null : 'DISTINCT',
42
            $this->getColumnsForQuery(current($countQuery->getRootAliases()))
43
        ));
44
45
        return $countQuery->resetDQLPart('orderBy')->getQuery()->getSingleScalarResult();
46
    }
47
48
    private function getColumnsForQuery($rootAlias)
49
    {
50
        if (\count($countColumns = $this->getCountColumn()) == 1) {
51
            return $rootAlias.'.'.current($countColumns);
52
        }
53
54
        $columns = implode(",'-',", array_map(function ($column) use ($rootAlias) {
55
            return "$rootAlias.$column";
56
        }, $countColumns));
57
58
        return "concat($columns)";
59
    }
60
61
    public function getResults($hydrationMode = Query::HYDRATE_OBJECT)
62
    {
63
        return $this->getQuery()->execute([], $hydrationMode);
64
    }
65
66
    public function getQuery()
67
    {
68
        return $this->query;
69
    }
70
71
    public function init()
72
    {
73
        $this->resetIterator();
74
75
        $this->setNbResults($this->computeNbResult());
76
77
        $this->getQuery()->setFirstResult(null);
78
        $this->getQuery()->setMaxResults(null);
79
80
        if (\count($this->getParameters()) > 0) {
81
            $this->getQuery()->setParameters($this->getParameters());
82
        }
83
84
        if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) {
85
            $this->setLastPage(0);
86
        } else {
87
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
88
89
            $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
90
91
            $this->getQuery()->setFirstResult($offset);
92
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
93
        }
94
    }
95
}
96