|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of EC-CUBE |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* http://www.lockon.co.jp/ |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
12
|
|
|
* of the License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program; if not, write to the Free Software |
|
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace Eccube\Doctrine\ORM\Tools; |
|
25
|
|
|
|
|
26
|
|
|
use Doctrine\ORM\Query; |
|
27
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
28
|
|
|
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper; |
|
29
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
|
30
|
|
|
|
|
31
|
|
|
class PaginatorAdapter implements PaginatorInterface |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var PaginatorInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $paginator; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* PaginatorAdapter constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param PaginatorInterface $paginator |
|
42
|
|
|
*/ |
|
43
|
19 |
|
public function __construct(PaginatorInterface $paginator) |
|
44
|
|
|
{ |
|
45
|
19 |
|
$this->paginator = $paginator; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
19 |
|
public function paginate($target, $page = 1, $limit = 10, array $options = array()) |
|
52
|
|
|
{ |
|
53
|
|
|
// wrap-queriesが未定義またはfalseの場合はデフォルトのページネータで処理. |
|
54
|
19 |
|
if (empty($options['wrap-queries'])) { |
|
55
|
2 |
|
return $this->paginator->paginate($target, $page, $limit, $options); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
17 |
|
if ($target instanceof QueryBuilder) { |
|
59
|
17 |
|
$target = $target->getQuery(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
17 |
|
if (!$target instanceof Query) { |
|
63
|
|
|
return $this->paginator->paginate($target, $page, $limit, $options); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// 総件数の取得. |
|
67
|
17 |
|
$platform = $target->getEntityManager()->getConnection()->getDatabasePlatform(); |
|
68
|
17 |
|
$rsm = new Query\ResultSetMapping(); |
|
69
|
17 |
|
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count'); |
|
70
|
|
|
// `wrap-queries: true`時の総件数取得クエリを再現するため,CountOutputWalkerを使う |
|
|
|
|
|
|
71
|
|
|
// CountOutputWalkerはdoctrine orm 2.5.xのコードを利用(2.4.xの不具合が修正されてる) |
|
72
|
17 |
|
$query = Helper::cloneQuery($target); |
|
73
|
17 |
|
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Eccube\Doctrine\ORM\Tools\Pagination\CountOutputWalker'); |
|
74
|
17 |
|
$query->setResultSetMapping($rsm); |
|
75
|
17 |
|
$totalCount = $query->getSingleScalarResult(); |
|
76
|
|
|
|
|
77
|
|
|
// limit, offsetの設定 |
|
78
|
17 |
|
$offset = abs($page - 1) * $limit; |
|
79
|
17 |
|
$target->setMaxResults($limit); |
|
80
|
17 |
|
$target->setFirstResult($offset); |
|
81
|
|
|
|
|
82
|
|
|
// pagenatorを空で作成し, 各オプションを設定する |
|
83
|
17 |
|
$pagination = $this->paginator->paginate(array()); |
|
84
|
17 |
|
$pagination->setCurrentPageNumber($page); |
|
85
|
17 |
|
$pagination->setItemNumberPerPage($limit); |
|
86
|
17 |
|
$pagination->setItems($target->getResult()); |
|
87
|
17 |
|
$pagination->setTotalItemCount($totalCount); |
|
88
|
|
|
|
|
89
|
17 |
|
return $pagination; |
|
90
|
|
|
} |
|
91
|
|
|
} |