public static function addOrderSort($sort, $table, &$query)
    {
        if (!empty($sort)) {
            $sorts = explode(',', $sort);
            foreach ($sorts as $sort) {
                if (!strpos($sort, '.')) {
                    preg_match('/\w+\s+(DESC|ASC)/', $sort, $sort_field);
                    $type = !empty($sort_field) ? trim($sort_field[1]) : 'DESC';
                    $field = !empty($sort_field) ? trim(substr($sort, 0, -strlen($type))) : trim($sort);
                    $order[$table . '.' . $field] = $type == 'DESC' ? SORT_DESC : SORT_ASC;
                } else {
                    $sort_table = trim(substr($sort, 0, strrpos($sort, '.')));
                    preg_match('/\w+\.\w+\s+(DESC|ASC)/', $sort, $sort_field);
                    $type = trim($sort_field[1]);
                    $field = trim(substr(substr($sort, strrpos($sort, '.') + 1), 0, -strlen($type)));
                    $order[trim($sort_table) . '.' . $field] = $type == 'DESC' ? SORT_DESC : SORT_ASC;;
                    $query->select[] = explode(' ', $sort_field[0])[0];
                    $query->joinWith($sort_table);
                }
            }
            $query->select[] = $table . ".*";
        }
        $query->orderBy($order);
    }
public static function addOrderSort($sort, $table, &$query)
    {
        if (!empty($sort)) {
            $sorts = explode(',', $sort);
            foreach ($sorts as $sort) {
                if (!strpos($sort, '.')) {
                    preg_match('/\w+\s+(DESC|ASC)/', $sort, $sort_field);
                    $type = !empty($sort_field) ? trim($sort_field[1]) : 'DESC';
                    $field = !empty($sort_field) ? trim(substr($sort, 0, -strlen($type))) : trim($sort);
                    $order[$table . '.' . $field] = $type == 'DESC' ? SORT_DESC : SORT_ASC;
                } else {
                    $sort_table = trim(substr($sort, 0, strrpos($sort, '.')));
                    preg_match('/\w+\.\w+\s+(DESC|ASC)/', $sort, $sort_field);
                    $type = trim($sort_field[1]);
                    $field = trim(substr(substr($sort, strrpos($sort, '.') + 1), 0, -strlen($type)));
                    $order[trim($sort_table) . '.' . $field] = $type == 'DESC' ? SORT_DESC : SORT_ASC;;
                    $query->select[] = explode(' ', $sort_field[0])[0];
                    $query->joinWith($sort_table);
                }
            }
            $query->select[] = $table . ".*";
        }
        $query->orderBy($order);
    }
public static function addOrderSort ( $ sort , $ table , & $ query ) { if ( ! empty ( $ sort ) ) { $ sorts = explode ( ',' , $ sort ) ; foreach ( $ sorts as $ sort ) { if ( ! strpos ( $ sort , '.' ) ) { preg_match ( '/\w+\s+(DESC|ASC)/' , $ sort , $ sort_field ) ; $ type = ! empty ( $ sort_field ) ? trim ( $ sort_field [ 1 ] ) : 'DESC' ; $ field = ! empty ( $ sort_field ) ? trim ( substr ( $ sort , 0 , - strlen ( $ type ) ) ) : trim ( $ sort ) ; $ order [ $ table . '.' . $ field ] = $ type == 'DESC' ? SORT_DESC : SORT_ASC ; } else { $ sort_table = trim ( substr ( $ sort , 0 , strrpos ( $ sort , '.' ) ) ) ; preg_match ( '/\w+\.\w+\s+(DESC|ASC)/' , $ sort , $ sort_field ) ; $ type = trim ( $ sort_field [ 1 ] ) ; $ field = trim ( substr ( substr ( $ sort , strrpos ( $ sort , '.' ) + 1 ) , 0 , - strlen ( $ type ) ) ) ; $ order [ trim ( $ sort_table ) . '.' . $ field ] = $ type == 'DESC' ? SORT_DESC : SORT_ASC ; ; $ query -> select [ ] = explode ( ' ' , $ sort_field [ 0 ] ) [ 0 ] ; $ query -> joinWith ( $ sort_table ) ; } } $ query -> select [ ] = $ table . ".*" ; } $ query -> orderBy ( $ order ) ; }
Add a sort if there is a sort oder requested.
@param $sort
@param $table
@param $query
Add a sort if there is a sort oder requested .