accounts/common/db/mysql/QueryBuilder.php

39 lines
1.1 KiB
PHP
Raw Normal View History

<?php
namespace common\db\mysql;
2018-04-18 02:17:25 +05:30
use yii\db\ExpressionInterface;
use yii\db\mysql\QueryBuilder as MysqlQueryBuilder;
class QueryBuilder extends MysqlQueryBuilder {
public function buildOrderBy($columns) {
if (empty($columns)) {
return '';
}
$orders = [];
2018-04-18 02:17:25 +05:30
foreach ($columns as $name => $direction) {
if ($direction instanceof ExpressionInterface) {
$orders[] = $direction->expression;
} elseif (is_array($direction)) {
2018-04-18 02:17:25 +05:30
// This condition branch is our custom solution
if (empty($direction)) {
continue;
}
$fieldValues = [];
2018-04-18 02:17:25 +05:30
foreach ($direction as $fieldValue) {
$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);
}
}