Completed
Pull Request — 3.x (#844)
by Marko
01:53
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
    public function getResults($hydrationMode = Query::HYDRATE_OBJECT)
49
    {
50
        return $this->getQuery()->execute([], $hydrationMode);
51
    }
52
53
    public function getQuery()
54
    {
55
        return $this->query;
56
    }
57
58
    public function init()
59
    {
60
        $this->resetIterator();
61
62
        $this->setNbResults($this->computeNbResult());
63
64
        $this->getQuery()->setFirstResult(null);
65
        $this->getQuery()->setMaxResults(null);
66
67
        if (\count($this->getParameters()) > 0) {
68
            $this->getQuery()->setParameters($this->getParameters());
69
        }
70
71
        if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) {
72
            $this->setLastPage(0);
73
        } else {
74
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
75
76
            $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
77
78
            $this->getQuery()->setFirstResult($offset);
79
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
80
        }
81
    }
82
83
    private function getColumnsForQuery($rootAlias)
84
    {
85
        if (1 === \count($countColumns = $this->getCountColumn())) {
86
            return $rootAlias.'.'.current($countColumns);
87
        }
88
89
        $columns = implode(",'-',", array_map(function ($column) use ($rootAlias) {
90
            return "$rootAlias.$column";
91
        }, $countColumns));
92
93
        return "concat($columns)";
94
    }
95
}
96