Skip to content

Commit e3d652b

Browse files
committed
docs: add missing yaml documentation in the default order section
1 parent e633059 commit e3d652b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

core/default-order.md

Lines changed: 51 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,20 @@ class Book
2830
}
2931
```
3032

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

45+
[codeSelector]
46+
3447
```php
3548
<?php
3649
// api/src/Entity/Book.php
@@ -59,8 +72,19 @@ class Book
5972
}
6073
```
6174

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

86+
[codeSelector]
87+
6488
```php
6589
<?php
6690
// api/src/Entity/Book.php
@@ -84,8 +108,19 @@ class Book
84108
}
85109
```
86110

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

122+
[codeSelector]
123+
89124
```php
90125
/**
91126
* collectionOperations={
@@ -106,3 +141,19 @@ class Book
106141
// ...
107142
}
108143
```
144+
145+
```yaml
146+
# config/api/resources/Book.yaml
147+
App\Entity\Book:
148+
get: ~
149+
get_desc_custom:
150+
method: get
151+
path: custom_collection_desc_foos
152+
order: {name: DESC}
153+
get_asc_custom:
154+
method: get
155+
path: custom_collection_asc_foos
156+
order: {name: ASC}
157+
```
158+
159+
[/codeSelector]

0 commit comments

Comments
 (0)