order方法调整

This commit is contained in:
thinkphp
2018-04-12 11:38:49 +08:00
parent b18863749e
commit 01acff377e
2 changed files with 31 additions and 37 deletions

View File

@@ -11,7 +11,6 @@
namespace think\db; namespace think\db;
use BadMethodCallException;
use PDO; use PDO;
use think\Exception; use think\Exception;
@@ -564,30 +563,25 @@ abstract class Builder
*/ */
protected function parseOrder($order, $options = []) protected function parseOrder($order, $options = [])
{ {
if (is_array($order)) {
$array = []; $array = [];
foreach ($order as $key => $val) { foreach ($order as $key => $val) {
if ($val instanceof Expression) { if ($val instanceof Expression) {
$array[] = $val->getValue(); $array[] = $val->getValue();
} elseif (is_numeric($key)) { } elseif ('[rand]' == $val) {
if ('[rand]' == $val) {
if (method_exists($this, 'parseRand')) {
$array[] = $this->parseRand(); $array[] = $this->parseRand();
} else { } else {
throw new BadMethodCallException('method not exists:' . get_class($this) . '-> parseRand'); if (is_numeric($key)) {
} list($key, $sort) = explode(' ', strpos($val, ' ') ? $val : $val . ' ');
} elseif (false === strpos($val, '(')) {
$array[] = $this->parseKey($val, $options);
} else { } else {
$array[] = $val; $sort = $val;
} }
} else {
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : ''; $sort = in_array(strtolower($sort), ['asc', 'desc'], true) ? ' ' . $sort : '';
$array[] = $this->parseKey($key, $options) . ' ' . $sort; $array[] = $this->parseKey($key, $options, true) . $sort;
} }
} }
$order = implode(',', $array); $order = implode(',', $array);
}
return !empty($order) ? ' ORDER BY ' . $order : ''; return !empty($order) ? ' ORDER BY ' . $order : '';
} }

View File

@@ -35,7 +35,7 @@ class Sqlsrv extends Builder
*/ */
protected function parseOrder($order, $options = []) protected function parseOrder($order, $options = [])
{ {
if (is_array($order)) {
$array = []; $array = [];
foreach ($order as $key => $val) { foreach ($order as $key => $val) {
if ($val instanceof Expression) { if ($val instanceof Expression) {
@@ -49,12 +49,12 @@ class Sqlsrv extends Builder
$array[] = $val; $array[] = $val;
} }
} else { } else {
$sort = in_array(strtolower(trim($val)), ['asc', 'desc']) ? ' ' . $val : ''; $sort = in_array(strtolower(trim($val)), ['asc', 'desc'], true) ? ' ' . $val : '';
$array[] = $this->parseKey($key, $options) . ' ' . $sort; $array[] = $this->parseKey($key, $options, true) . ' ' . $sort;
} }
} }
$order = implode(',', $array); $order = implode(',', $array);
}
return !empty($order) ? ' ORDER BY ' . $order : ' ORDER BY rand()'; return !empty($order) ? ' ORDER BY ' . $order : ' ORDER BY rand()';
} }