Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public function getIndex(array $queryParams = [])
{
$options = array_merge([
'limit' => null,
'page' => null, // ページ番号
'direction' => '', // 並び方向
'order' => '', // 並び順対象のフィールド
'contain' => [],
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Loading