2016-09-21 13:42:40 +05:30
|
|
|
<?php
|
2019-09-18 04:44:05 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-09-21 13:42:40 +05:30
|
|
|
namespace common\db\mysql;
|
|
|
|
|
2020-06-10 20:08:05 +05:30
|
|
|
use SamIT\Yii2\MariaDb\QueryBuilder as MariaDbQueryBuilder;
|
2024-12-02 15:40:55 +05:30
|
|
|
use yii\db\Expression;
|
2016-09-21 13:42:40 +05:30
|
|
|
|
2020-06-10 20:08:05 +05:30
|
|
|
class QueryBuilder extends MariaDbQueryBuilder {
|
2016-09-21 13:42:40 +05:30
|
|
|
|
|
|
|
public function buildOrderBy($columns) {
|
|
|
|
if (empty($columns)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$orders = [];
|
2018-04-18 02:17:25 +05:30
|
|
|
foreach ($columns as $name => $direction) {
|
2024-12-02 15:40:55 +05:30
|
|
|
if ($direction instanceof Expression) {
|
2016-09-21 13:42:40 +05:30
|
|
|
$orders[] = $direction->expression;
|
|
|
|
} elseif (is_array($direction)) {
|
2018-04-18 02:17:25 +05:30
|
|
|
// This condition branch is our custom solution
|
2016-09-21 13:42:40 +05:30
|
|
|
if (empty($direction)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$fieldValues = [];
|
2018-04-18 02:17:25 +05:30
|
|
|
foreach ($direction as $fieldValue) {
|
2016-09-21 13:42:40 +05:30
|
|
|
$fieldValues[] = $this->db->quoteValue($fieldValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
$orders[] = 'FIELD(' . $this->db->quoteColumnName($name) . ',' . implode(',', $fieldValues) . ')';
|
|
|
|
} else {
|
|
|
|
$orders[] = $this->db->quoteColumnName($name) . ($direction === SORT_DESC ? ' DESC' : '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'ORDER BY ' . implode(', ', $orders);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|