Skip to content

Commit 9edb189

Browse files
johnkrovitchalanpoulain
authored andcommitted
docs: add missing yaml documentation in the default order section
1 parent 7c1d753 commit 9edb189

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

core/default-order.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ API Platform Core provides an easy way to override the default order of items in
55
By default, items in the collection are ordered in ascending (ASC) order by their resource identifier(s). If you want to
66
customize this order, you must add an `order` attribute on your ApiResource annotation:
77

8+
[codeSelector]
9+
810
```php
911
<?php
1012
// api/src/Entity/Book.php
@@ -28,9 +30,21 @@ class Book
2830
}
2931
```
3032

33+
```yaml
34+
# config/api/resources/Book.yaml
35+
App\Entity\Book:
36+
attributes:
37+
order:
38+
foo: ASC
39+
```
40+
41+
[/codeSelector]
42+
3143
This `order` attribute is used as an array: the key defines the order field, the values defines the direction.
3244
If you only specify the key, `ASC` direction will be used as default. For example, to order by `foo` & `bar`:
3345

46+
[codeSelector]
47+
3448
```php
3549
<?php
3650
// api/src/Entity/Book.php
@@ -59,8 +73,19 @@ class Book
5973
}
6074
```
6175

76+
```yaml
77+
# config/api/resources/Book.yaml
78+
App\Entity\Book:
79+
attributes:
80+
order: ['foo', 'bar']
81+
```
82+
83+
[/codeSelector]
84+
6285
It's also possible to configure the default order on an association property:
6386

87+
[codeSelector]
88+
6489
```php
6590
<?php
6691
// api/src/Entity/Book.php
@@ -84,8 +109,19 @@ class Book
84109
}
85110
```
86111

112+
```yaml
113+
# config/api/resources/Book.yaml
114+
App\Entity\Book:
115+
attributes:
116+
order: ['author.username']
117+
```
118+
119+
[/codeSelector]
120+
87121
Another possibility is to apply the default order for a specific collection operation, which will override the global default order configuration.
88122

123+
[codeSelector]
124+
89125
```php
90126
/**
91127
* collectionOperations={
@@ -106,3 +142,21 @@ class Book
106142
// ...
107143
}
108144
```
145+
146+
```yaml
147+
# config/api/resources/Book.yaml
148+
App\Entity\Book:
149+
get: ~
150+
get_desc_custom:
151+
method: get
152+
path: custom_collection_desc_foos
153+
order:
154+
name: DESC
155+
get_asc_custom:
156+
method: get
157+
path: custom_collection_asc_foos
158+
order:
159+
name: ASC
160+
```
161+
162+
[/codeSelector]

0 commit comments

Comments
 (0)