Skip to content

Commit

Permalink
fix book search with orderby
Browse files Browse the repository at this point in the history
  • Loading branch information
ufaboy committed Oct 21, 2023
1 parent acd50c3 commit a1ec433
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
4 changes: 4 additions & 0 deletions common/models/BookSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public function search($params): ActiveDataProvider {
'asc' => ['series.name' => SORT_ASC],
'desc' => ['series.name' => SORT_DESC],
];
$dataProvider->sort->attributes['rating'] = [
'asc' => [new \yii\db\Expression('rating ASC NULLS FIRST'), 'book.id' => SORT_ASC],
'desc' => [new \yii\db\Expression('rating DESC NULLS LAST'), 'book.id' => SORT_DESC],
];
return $dataProvider;
}
}
34 changes: 23 additions & 11 deletions modules/api/models/BookSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\api\models\Book;
use yii\db\Expression;

/**
* BookSearch represents the model behind the search form of `app\modules\api\models\Book`.
Expand Down Expand Up @@ -138,26 +139,37 @@ public function search($params) {

$query->groupBy(['book.id', 'author.name', 'series.name']);
$dataProvider->sort->attributes['length'] = [
'asc' => ['length' => SORT_ASC],
'desc' => ['length' => SORT_DESC],
'asc' => ['length' => SORT_ASC, 'book.id' => SORT_ASC],
'desc' => ['length' => SORT_DESC, 'book.id' => SORT_DESC],
];
$dataProvider->sort->attributes['tags'] = [
'asc' => ['tag_count' => SORT_ASC],
'desc' => ['tag_count' => SORT_DESC],
'asc' => ['tag_count' => SORT_ASC, 'book.id' => SORT_ASC],
'desc' => ['tag_count' => SORT_DESC, 'book.id' => SORT_DESC],
];
$dataProvider->sort->attributes['author'] = [
'asc' => ['author.name' => SORT_ASC],
'desc' => ['author.name' => SORT_DESC],
'asc' => ['author.name' => SORT_ASC, 'book.id' => SORT_ASC],
'desc' => ['author.name' => SORT_DESC, 'book.id' => SORT_DESC],
];
$dataProvider->sort->attributes['series'] = [
'asc' => ['series.name' => SORT_ASC],
'desc' => ['series.name' => SORT_DESC],
'asc' => ['series.name' => SORT_ASC, 'book.id' => SORT_ASC],
'desc' => ['series.name' => SORT_DESC, 'book.id' => SORT_DESC],
];
$dataProvider->sort->attributes['rating'] = [
'asc' => [new \yii\db\Expression('rating ASC NULLS FIRST'),],
'desc' => [new \yii\db\Expression('rating DESC NULLS LAST'),],
'asc' => [new \yii\db\Expression('rating ASC NULLS FIRST'), 'book.id' => SORT_ASC],
'desc' => [new \yii\db\Expression('rating DESC NULLS LAST'), 'book.id' => SORT_DESC],
];
$dataProvider->sort->attributes['updated_at'] = [
'asc' => [new \yii\db\Expression('updated_at ASC NULLS FIRST'), 'book.id' => SORT_ASC],
'desc' => [new \yii\db\Expression('updated_at DESC NULLS LAST'), 'book.id' => SORT_DESC],
];
$dataProvider->sort->attributes['view_count'] = [
'asc' => [new \yii\db\Expression('view_count ASC NULLS FIRST'), 'book.id' => SORT_ASC],
'desc' => [new \yii\db\Expression('view_count DESC NULLS LAST'), 'book.id' => SORT_DESC],
];
$dataProvider->sort->attributes['last_read'] = [
'asc' => [new \yii\db\Expression('last_read ASC NULLS FIRST'), 'book.id' => SORT_ASC],
'desc' => [new \yii\db\Expression('last_read DESC NULLS LAST'), 'book.id' => SORT_DESC],
];

return $dataProvider;
}
}

0 comments on commit a1ec433

Please sign in to comment.