|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Eccube\Doctrine\ORM\Tools; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\Query; |
|
6
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
7
|
|
|
use Knp\Component\Pager\Event\Subscriber\Paginate\Doctrine\ORM\Query\Helper; |
|
8
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
|
9
|
|
|
|
|
10
|
|
|
class PaginatorAdapter implements PaginatorInterface |
|
|
|
|
|
|
11
|
|
|
{ |
|
12
|
|
|
protected $paginator; |
|
13
|
|
|
|
|
14
|
19 |
|
public function __construct(PaginatorInterface $paginator) |
|
|
|
|
|
|
15
|
|
|
{ |
|
16
|
19 |
|
$this->paginator = $paginator; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
19 |
|
public function paginate($target, $page = 1, $limit = 10, array $options = array()) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
// wrap-queriesが未定義またはfalseの場合はデフォルトのページネータで処理. |
|
22
|
19 |
|
if (empty($options['wrap-queries'])) { |
|
23
|
11 |
|
return $this->paginator->paginate($target, $page, $limit, $options); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
8 |
|
if ($target instanceof QueryBuilder) { |
|
27
|
8 |
|
$target = $target->getQuery(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
8 |
|
if (!$target instanceof Query) { |
|
31
|
|
|
return $this->paginator->paginate($target, $page, $limit, $options); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// 総件数の取得. |
|
35
|
8 |
|
$platform = $target->getEntityManager()->getConnection()->getDatabasePlatform(); |
|
36
|
8 |
|
$rsm = new Query\ResultSetMapping(); |
|
37
|
8 |
|
$rsm->addScalarResult($platform->getSQLResultCasing('dctrn_count'), 'count'); |
|
38
|
|
|
// `wrap-queries: true`時の総件数取得クエリを再現するため,CountOutputWalkerを使う |
|
|
|
|
|
|
39
|
|
|
// CountOutputWalkerはdoctrine orm 2.5.xのコードを利用(2.4.xの不具合が修正されてる) |
|
40
|
8 |
|
$query = Helper::cloneQuery($target); |
|
41
|
8 |
|
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'Eccube\Doctrine\ORM\Tools\Pagination\CountOutputWalker'); |
|
42
|
8 |
|
$query->setResultSetMapping($rsm); |
|
43
|
8 |
|
$totalCount = $query->getSingleScalarResult(); |
|
44
|
|
|
|
|
45
|
|
|
// limit, offsetの設定 |
|
46
|
8 |
|
$offset = abs($page - 1) * $limit; |
|
47
|
8 |
|
$target->setMaxResults($limit); |
|
48
|
8 |
|
$target->setFirstResult($offset); |
|
49
|
|
|
|
|
50
|
|
|
// pagenatorを空で作成し, 各オプションを設定する |
|
51
|
8 |
|
$pagination = $this->paginator->paginate(array()); |
|
52
|
8 |
|
$pagination->setCurrentPageNumber($page); |
|
53
|
8 |
|
$pagination->setItemNumberPerPage($limit); |
|
54
|
8 |
|
$pagination->setItems($target->getResult()); |
|
55
|
8 |
|
$pagination->setTotalItemCount($totalCount); |
|
56
|
|
|
|
|
57
|
8 |
|
return $pagination; |
|
58
|
|
|
} |
|
59
|
|
|
} |