1
- # Override default order
1
+ # Override Default Order
2
2
3
3
API Platform Core provides an easy way to override default order in your collection.
4
4
@@ -11,26 +11,77 @@ By default, it will order by resource identifier(s) using ASC direction. If you
11
11
12
12
namespace AppBundle\Entity;
13
13
14
- use ApiPlatform\Core\Annotation\ApiProperty;
15
14
use ApiPlatform\Core\Annotation\ApiResource;
16
15
17
16
/**
18
- * @ApiResource(attributes={"order"={"foo", "ASC"}})
17
+ * @ApiResource(attributes={"order"={"foo": "ASC"}})
19
18
*/
20
19
class Book
21
20
{
22
21
// ...
23
22
24
23
/**
25
24
* ...
26
- * @ApiProperty()
27
25
*/
28
26
public $foo;
29
27
}
30
28
```
31
29
32
- This ` order ` attribute is used as an array with 2 entries: the first one defines the order field, the second one defined the direction.
33
- If you only specify the first one, ` ASC ` direction will be used as default.
30
+ This ` order ` attribute is used as an array: the key defines the order field, the values defines the direction.
31
+ If you only specify the key, ` ASC ` direction will be used as default. For example, to order by ` foo ` & ` bar ` :
32
+
33
+ ``` php
34
+ <?php
35
+
36
+ // src/AppBundle/Entity/Book.php
37
+
38
+ namespace AppBundle\Entity;
39
+
40
+ use ApiPlatform\Core\Annotation\ApiResource;
41
+
42
+ /**
43
+ * @ApiResource(attributes={"order"={"foo", "bar"}})
44
+ */
45
+ class Book
46
+ {
47
+ // ...
48
+
49
+ /**
50
+ * ...
51
+ */
52
+ public $foo;
53
+
54
+ /**
55
+ * ...
56
+ */
57
+ public $bar;
58
+ }
59
+ ```
60
+
61
+ It's also possible to configure the default filter on an association property:
62
+
63
+ ``` php
64
+ <?php
65
+
66
+ // src/AppBundle/Entity/Book.php
67
+
68
+ namespace AppBundle\Entity;
69
+
70
+ use ApiPlatform\Core\Annotation\ApiResource;
71
+
72
+ /**
73
+ * @ApiResource(attributes={"order"={"author.username"}})
74
+ */
75
+ class Book
76
+ {
77
+ // ...
78
+
79
+ /**
80
+ * @var User
81
+ */
82
+ public $author;
83
+ }
84
+ ```
34
85
35
86
Previous chapter: [ Configuration] ( configuration.md )
36
87
0 commit comments