Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
open-source
uql
Commits
5f31c88c
Commit
5f31c88c
authored
5 years ago
by
lhy
Browse files
Options
Download
Email Patches
Plain Diff
Added reverse sort for nullable field
parent
4a24fc94
null-sort
1 merge request
!10
Null Sort Latest
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
5 deletions
+38
-5
src/Sort.php
src/Sort.php
+16
-2
src/capsule/CapsuleCompiler.php
src/capsule/CapsuleCompiler.php
+5
-1
src/yii2/Yii2Compiler.php
src/yii2/Yii2Compiler.php
+3
-0
tests/SortTest.php
tests/SortTest.php
+14
-2
No files found.
src/Sort.php
View file @
5f31c88c
...
...
@@ -52,6 +52,9 @@ class Sort extends BaseObject
$field
=
substr
(
$field
,
1
);
$sort
=
'desc'
;
}
if
(
$this
->
isStarredNullField
(
$field
))
{
$sort
=
$sort
===
'asc'
?
'desc'
:
'asc'
;
}
}
$params
[
$field
]
=
$sort
;
...
...
@@ -60,6 +63,11 @@ class Sort extends BaseObject
return
$params
;
}
private
function
isStarredNullField
(
$field
)
{
return
$field
[
strlen
(
$field
)
-
1
]
===
'*'
;
}
public
function
getOrders
()
{
$rules
=
$this
->
normalizedSortRules
();
...
...
@@ -67,11 +75,17 @@ class Sort extends BaseObject
$orders
=
[];
foreach
(
$params
as
$field
=>
$sort
)
{
if
(
!
isset
(
$rules
[
$field
]))
{
$ruleField
=
$field
;
if
(
$this
->
isStarredNullField
(
$field
))
{
$ruleField
=
substr
(
$field
,
0
,
strlen
(
$field
)
-
1
);
$field
=
'-'
.
$ruleField
;
}
if
(
!
isset
(
$rules
[
$ruleField
]))
{
continue
;
}
$sortDef
=
$rules
[
$
f
ield
];
$sortDef
=
$rules
[
$
ruleF
ield
];
if
(
$sortDef
&&
is_callable
(
$sortDef
))
{
$orders
[
$field
]
=
$sortDef
;
...
...
This diff is collapsed.
Click to expand it.
src/capsule/CapsuleCompiler.php
View file @
5f31c88c
...
...
@@ -61,7 +61,11 @@ class CapsuleCompiler implements Compiler
public
function
buildSort
(
array
$orders
)
{
foreach
(
$orders
as
$field
=>
$direction
)
{
$this
->
query
->
orderBy
(
$field
,
$direction
);
if
(
$field
[
0
]
===
'-'
)
{
$this
->
query
->
orderByRaw
(
"
$field
$direction
"
);
}
else
{
$this
->
query
->
orderBy
(
$field
,
$direction
);
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/yii2/Yii2Compiler.php
View file @
5f31c88c
...
...
@@ -67,6 +67,9 @@ class Yii2Compiler implements Compiler
];
foreach
(
$orders
as
$key
=>
$order
)
{
if
(
$key
[
0
]
===
'-'
)
{
throw
new
\
InvalidArgumentException
(
'Unsupported yet.'
);
}
$orders
[
$key
]
=
$orderMap
[
$order
]
??
$order
;
}
...
...
This diff is collapsed.
Click to expand it.
tests/SortTest.php
View file @
5f31c88c
...
...
@@ -33,7 +33,7 @@ class SortTest extends \PHPUnit_Framework_TestCase
[
''
,
[],
]
]
,
];
}
...
...
@@ -90,7 +90,19 @@ class SortTest extends \PHPUnit_Framework_TestCase
[
'name'
=>
'asc'
,
'gender'
=>
'desc'
,
]
],
],
// reverse null sort
[
'expired_at*,-gender'
,
[
'expired_at'
,
'gender'
,
],
[
'-expired_at'
=>
'desc'
,
'gender'
=>
'desc'
,
],
],
];
}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment