Completed
Push — 3.x-dev-kit ( 4fe8f1 )
by
unknown
11:30 queued 08:33
created

Pager::getResults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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
     * @deprecated This property is deprecated since version 2.4 and will be removed in 3.0.
26
     */
27
    protected $queryBuilder = null;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function computeNbResult()
33
    {
34
        $countQuery = clone $this->getQuery();
35
36
        if (count($this->getParameters()) > 0) {
37
            $countQuery->setParameters($this->getParameters());
38
        }
39
40
        $countQuery->select(sprintf('count(DISTINCT %s.%s) as cnt', $countQuery->getRootAlias(), current($this->getCountColumn())));
41
42
        return $countQuery->resetDQLPart('orderBy')->getQuery()->getSingleScalarResult();
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getResults($hydrationMode = Query::HYDRATE_OBJECT)
49
    {
50
        return $this->getQuery()->execute(array(), $hydrationMode);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getQuery()
57
    {
58
        return $this->query;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function init()
65
    {
66
        $this->resetIterator();
67
68
        $this->setNbResults($this->computeNbResult());
69
70
        $this->getQuery()->setFirstResult(null);
71
        $this->getQuery()->setMaxResults(null);
72
73
        if (count($this->getParameters()) > 0) {
74
            $this->getQuery()->setParameters($this->getParameters());
75
        }
76
77
        if (0 == $this->getPage() || 0 == $this->getMaxPerPage() || 0 == $this->getNbResults()) {
78
            $this->setLastPage(0);
79
        } else {
80
            $offset = ($this->getPage() - 1) * $this->getMaxPerPage();
81
82
            $this->setLastPage(ceil($this->getNbResults() / $this->getMaxPerPage()));
83
84
            $this->getQuery()->setFirstResult($offset);
85
            $this->getQuery()->setMaxResults($this->getMaxPerPage());
86
        }
87
    }
88
}
89