orderBy

用于对操作的结果排序。

基本用法

默认DESC排序规则

  1. $builder->orderBy('col1', 'ASC')->get('getTable');
  2. $builder->where('col1',2)->orderBy('col1', 'ASC')->get('getTable');

ORDER BY FIELD

可以通过第三个参数实现

  1. $array = [
  2. 'a',
  3. 'b'
  4. ];
  5. $builder->orderBy('', 'DESC', $array)->get('getTable');

实现原理

  1. if (is_array($customFieldsOrRegExp)) {
  2. foreach ($customFieldsOrRegExp as $key => $value) {
  3. $customFieldsOrRegExp[$key] = preg_replace("/[^\x80-\xff-a-z0-9\.\(\),_` ]+/i", '', $value);
  4. }
  5. $orderByField = 'FIELD (' . $orderByField . ', "' . implode('","', $customFieldsOrRegExp) . '")';
  6. }

ORDER BY REGEXP

可以通过第三个参数实现

实现原理

  1. if (is_string($customFieldsOrRegExp)) {
  2. $orderByField = $orderByField . " REGEXP '" . $customFieldsOrRegExp . "'";
  3. }

传参说明

方法原型

  1. function orderBy($orderByField, $orderbyDirection = "DESC", $customFieldsOrRegExp = null)
  • $orderByField 排序字段
  • $orderbyDirection 排序规则
  • $customFieldsOrRegExp 其他条件