Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.

Commit

Permalink
改进
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Oct 8, 2018
1 parent 7730672 commit 9748cb8
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions ThinkPHP/Library/Think/Db/Driver.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,29 +758,29 @@ protected function parseOrder($order)
return '';
}
$array = array();
if (is_string($order) && '[RAND]' != $order) {
$order = explode(',', $order);
}

if (is_array($order)) {
foreach ($order as $key => $val) {
if (is_numeric($key)) {
if (false === strpos($val, '(') && false === strpos($val, ';')) {
$array[] = $this->parseKey($val);
}
} elseif (false === strpos($key, ')') && false === strpos($key, '#')) {
$sort = in_array(strtolower($val), array('asc', 'desc')) ? ' ' . $val : '';
list($key, $sort) = explode(' ', strpos($val, ' ') ? $val : $val . ' ');
} else {
$sort = $val;
}

if (preg_match('/^[\w]+$/', $key)) {
$sort = strtoupper($sort);
$sort = in_array($sort, ['ASC', 'DESC'], true) ? ' ' . $sort : '';
$array[] = $this->parseKey($key, true) . $sort;
}
}
} elseif ('[RAND]' == $order) {
// 随机排序
$array[] = $this->parseRand();
} else {
foreach (explode(',', $order) as $val) {
if (preg_match('/\s+(ASC|DESC)$/i', rtrim($val), $match, PREG_OFFSET_CAPTURE)) {
$array[] = $this->parseKey(ltrim(substr($val, 0, $match[0][1]))) . ' ' . $match[1][0];
} elseif (false === strpos($val, '(') && false === strpos($val, ';')) {
$array[] = $this->parseKey($val);
}
}
}

$order = implode(',', $array);
return !empty($order) ? ' ORDER BY ' . $order : '';
}
Expand Down

1 comment on commit 9748cb8

@yuanzhihai
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

缺少这个 parseRand 方法

Please sign in to comment.