diff --git a/plugins/bc-custom-content/src/Service/CustomEntriesService.php b/plugins/bc-custom-content/src/Service/CustomEntriesService.php index 92b07928aa..c91db1d260 100644 --- a/plugins/bc-custom-content/src/Service/CustomEntriesService.php +++ b/plugins/bc-custom-content/src/Service/CustomEntriesService.php @@ -160,6 +160,7 @@ public function getIndex(array $queryParams = []) { $options = array_merge([ 'limit' => null, + 'page' => null, // ページ番号 'direction' => '', // 並び方向 'order' => '', // 並び順対象のフィールド 'contain' => [], @@ -180,10 +181,14 @@ public function getIndex(array $queryParams = []) } if (!empty($options['limit'])) { - $query->limit($options['limit']); + if (!empty($options['page'])) { + $query->page($options['page'], $options['limit']); + } else { + $query->limit($options['limit']); + } } - unset($options['order'], $options['direction'], $options['limit']); + unset($options['order'], $options['direction'], $options['limit'], $options['page']); if (!empty($options)) { $query = $this->createIndexConditions($query, $options); diff --git a/plugins/bc-custom-content/tests/TestCase/Service/CustomEntriesServiceTest.php b/plugins/bc-custom-content/tests/TestCase/Service/CustomEntriesServiceTest.php index de3d255375..7b2df40ef5 100644 --- a/plugins/bc-custom-content/tests/TestCase/Service/CustomEntriesServiceTest.php +++ b/plugins/bc-custom-content/tests/TestCase/Service/CustomEntriesServiceTest.php @@ -257,6 +257,19 @@ public function test_getIndex() $result = $this->CustomEntriesService->getIndex(['order' => 'name', 'direction' => 'desc'])->all()->toArray(); $this->assertEquals('プログラマー 3', $result[0]->name); + //pageパラメータを入れる + $params = ['limit' => 2, 'order' => 'id', 'direction' => 'asc']; + $page1 = $this->CustomEntriesService->getIndex($params + ['page' => 1])->all()->toArray(); + $page2 = $this->CustomEntriesService->getIndex($params + ['page' => 2])->all()->toArray(); + //全3件を2件ずつに分割して取得できる + $this->assertCount(2, $page1); + $this->assertCount(1, $page2); + //2ページ目には1ページ目と異なるデータが返る + $this->assertNotContains($page2[0]->id, [$page1[0]->id, $page1[1]->id]); + + //limitがない場合、pageパラメータは無視する + $result = $this->CustomEntriesService->getIndex(['page' => 2])->all(); + $this->assertCount(3, $result); } /**