source: pro-violet-viettel/sourcecode/application/libraries/Doctrine/ORM/Tools/Pagination/CountWalker.php @ 345

Last change on this file since 345 was 345, checked in by quyenla, 11 years ago

collaborator page

File size: 2.7 KB
Line 
1<?php
2/**
3 * Doctrine ORM
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to kontakt@beberlei.de so I can send you a copy immediately.
12 */
13
14namespace Doctrine\ORM\Tools\Pagination;
15
16use Doctrine\ORM\Query\TreeWalkerAdapter,
17    Doctrine\ORM\Query\AST\SelectStatement,
18    Doctrine\ORM\Query\AST\SelectExpression,
19    Doctrine\ORM\Query\AST\PathExpression,
20    Doctrine\ORM\Query\AST\AggregateExpression;
21
22/**
23 * Replaces the selectClause of the AST with a COUNT statement
24 *
25 * @category    DoctrineExtensions
26 * @package     DoctrineExtensions\Paginate
27 * @author      David Abdemoulaie <dave@hobodave.com>
28 * @copyright   Copyright (c) 2010 David Abdemoulaie (http://hobodave.com/)
29 * @license     http://hobodave.com/license.txt New BSD License
30 */
31class CountWalker extends TreeWalkerAdapter
32{
33    /**
34     * Distinct mode hint name
35     */
36    const HINT_DISTINCT = 'doctrine_paginator.distinct';
37
38    /**
39     * Walks down a SelectStatement AST node, modifying it to retrieve a COUNT
40     *
41     * @param SelectStatement $AST
42     * @return void
43     */
44    public function walkSelectStatement(SelectStatement $AST)
45    {
46        $rootComponents = array();
47        foreach ($this->_getQueryComponents() AS $dqlAlias => $qComp) {
48            $isParent = array_key_exists('parent', $qComp)
49                && $qComp['parent'] === null
50                && $qComp['nestingLevel'] == 0
51            ;
52            if ($isParent) {
53                $rootComponents[] = array($dqlAlias => $qComp);
54            }
55        }
56        if (count($rootComponents) > 1) {
57            throw new \RuntimeException("Cannot count query which selects two FROM components, cannot make distinction");
58        }
59        $root = reset($rootComponents);
60        $parentName = key($root);
61        $parent = current($root);
62
63        $pathExpression = new PathExpression(
64            PathExpression::TYPE_STATE_FIELD | PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $parentName,
65            $parent['metadata']->getSingleIdentifierFieldName()
66        );
67        $pathExpression->type = PathExpression::TYPE_STATE_FIELD;
68
69        $distinct = $this->_getQuery()->getHint(self::HINT_DISTINCT);
70        $AST->selectClause->selectExpressions = array(
71            new SelectExpression(
72                new AggregateExpression('count', $pathExpression, $distinct), null
73            )
74        );
75
76        // ORDER BY is not needed, only increases query execution through unnecessary sorting.
77        $AST->orderByClause = null;
78    }
79}
80
Note: See TracBrowser for help on using the repository browser.