Last active
March 7, 2021 04:18
-
-
Save cureseven/99b91e7daa5a2ac61445f9112d298df7 to your computer and use it in GitHub Desktop.
cakephp3.0のpaginateをみた
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function paginate($object, array $settings = []) | |
{ | |
// 第1引数がQueryInterfaceを実装したobjectだったら | |
// RepositoryInterfaceを実装したオブジェクトを呼び出す | |
if ($object instanceof QueryInterface) { | |
$query = $object; | |
$object = $query->repository(); | |
} | |
// ページネーションの設定まとめた$optionを作成 | |
$alias = $object->alias(); | |
$options = $this->mergeOptions($alias, $settings); | |
$options = $this->validateSort($object, $options); | |
$options = $this->checkLimit($options); | |
// 最初のif文(QueryInterface)に必要なものらしい https://github.com/cakephp/cakephp/commit/39a42421cc9e0e3c73ee7c25f7c61ac5ed252c4f | |
$options += ['page' => 1]; | |
$options['page'] = (int)$options['page'] < 1 ? 1 : (int)$options['page']; // intは早い | |
list($finder, $options) = $this->_extractFinder($options); // finder名だけ抽出 | |
// optionを適応する | |
if (empty($query)) { | |
$query = $object->find($finder, $options); | |
} else { | |
// QueryInterfaceの場合 | |
$query->applyOptions($options); | |
} | |
// データ件数取得 | |
$results = $query->all(); | |
$numResults = count($results); | |
$count = $numResults ? $query->count() : 0; //カウントする | |
// デフォルトのsettingを適応 | |
$defaults = $this->getDefaults($alias, $settings); | |
unset($defaults[0]); | |
$page = $options['page']; | |
$limit = $options['limit']; // 1ページあたりの最大件数 | |
$pageCount = (int)ceil($count / $limit); // 切り上げ。ページ数を決定 | |
$requestedPage = $page; | |
$page = max(min($page, $pageCount), 1); // ないページにアクセスしないように | |
$request = $this->_registry->getController()->request; // 呼び出しもとからのrequestをとってくる | |
// orderの指定があったら適応 | |
$order = (array)$options['order']; | |
$sortDefault = $directionDefault = false; | |
if (!empty($defaults['order']) && count($defaults['order']) == 1) { | |
// Helperのため | |
// https://github.com/cakephp/cakephp/commit/673638a36221627943346bf14909e0f50d18daab | |
$sortDefault = key($defaults['order']); | |
$directionDefault = current($defaults['order']); | |
} | |
$paging = [ | |
'finder' => $finder, | |
'page' => $page, | |
'current' => $numResults, | |
'count' => $count, | |
'perPage' => $limit, | |
'prevPage' => ($page > 1), | |
'nextPage' => ($count > ($page * $limit)), | |
'pageCount' => $pageCount, | |
'sort' => key($order), | |
'direction' => current($order), | |
'limit' => $defaults['limit'] != $limit ? $limit : null, | |
'sortDefault' => $sortDefault, | |
'directionDefault' => $directionDefault | |
]; | |
if (!isset($request['paging'])) { | |
$request['paging'] = []; | |
} | |
$request['paging'] = [$alias => $paging] + (array)$request['paging']; //あとで使ってないのになんでこんなことしてるんだ・・ | |
// 想定してなかったパターンの例外処理 | |
if ($requestedPage > $page) { | |
throw new NotFoundException(); | |
} | |
return $results; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment