SELECT * FROM "related_item" ORDER BY position = 2 DESC, position = 1 DESC, position = 3 ASC
class Sort extends \yii\data\Sort
{
/**
* @inheritdoc
*/
public function getOrders($recalculate = false): array
{
$orders = [];
$request = Yii::$app->getRequest();
$params = $request->getQueryParams();
if (isset($params[$this->sortParam])) {
$sortParams = explode(',', $params[$this->sortParam]);
foreach ($sortParams as $rawField) {
$field = explode(':', ltrim($rawField, '-'));
$sort = 'ASC';
if (($rawField{0} == '-')) {
$sort = 'DESC';
}
if (in_array($field[0], array_keys($this->attributes)) && !empty($field[1])) {
$value = (int) $field[1];
$orders[] = (new Expression("$field[0] = $value $sort"));
}
}
}
$orders = ArrayHelper::merge(
$orders,
parent::getOrders($recalculate)
);
return $orders;
}
}
'query' => SomeModel::find()->orderBy('position = 2 DESC, position = 1 DESC, position = 3 ASC')
$sort = new Sort([
'attributes' => [
'position' => [
'default' => 'position = 2 DESC, position = 1 DESC, position = 3 ASC',
],
],
]);
'name' => [
'asc' => '[[last_name]] ASC NULLS FIRST', // PostgreSQL specific feature
'desc' => '[[last_name]] DESC NULLS LAST',
]