Skip to content

Commit 3a1afe1

Browse files
vincentchalamonmeyerbaptiste
authored andcommitted
Fix api-platform/core #1246: configure default order on ApiResource annotation
1 parent 9513635 commit 3a1afe1

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

core/configuration.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,4 @@ api_platform:
176176

177177
Previous chapter: [Getting Started](getting-started.md)
178178

179-
Next chapter: [Operations](operations.md)
179+
Next chapter: [Override default order](default-order.md)

core/default-order.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Override default order
2+
3+
API Platform Core provides an easy way to override default order in your collection.
4+
5+
By default, it will order by resource identifier(s) using ASC direction. If you want to customize this order, you must add an `order` attribute on your ApiResource annotation:
6+
7+
```php
8+
<?php
9+
10+
// src/AppBundle/Entity/Book.php
11+
12+
namespace AppBundle\Entity;
13+
14+
use ApiPlatform\Core\Annotation\ApiProperty;
15+
use ApiPlatform\Core\Annotation\ApiResource;
16+
17+
/**
18+
* @ApiResource(attributes={"order"={"foo", "ASC"}})
19+
*/
20+
class Book
21+
{
22+
// ...
23+
24+
/**
25+
* ...
26+
* @ApiProperty()
27+
*/
28+
public $foo;
29+
}
30+
```
31+
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.
34+
35+
Previous chapter: [Configuration](configuration.md)
36+
37+
Next chapter: [Operations](operations.md)

index.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
3. [Creating Custom Filters](core/filters.md#creating-custom-filters)
3636
1. [Creating Custom Doctrine ORM Filters](core/filters.md#creating-custom-doctrine-orm-filters)
3737
2. [Overriding Extraction of Properties from the Request](core/filters.md#overriding-extraction-of-properties-from-the-request)
38-
6. [Serialization Groups and Relations](core/serialization-groups-and-relations.md)
38+
6. [Override Default Order](core/default-order.md)
39+
7. [Serialization Groups and Relations](core/serialization-groups-and-relations.md)
3940
1. [Configuration](core/serialization-groups-and-relations.md#configuration)
4041
2. [Using Different Serialization Groups per Operation](core/serialization-groups-and-relations.md#embedding-relations)
4142
3. [Embedding Relations](core/serialization-groups-and-relations.md#embedding-relations)
@@ -46,24 +47,24 @@
4647
5. [Entity Identifier Case](core/serialization-groups-and-relations.md#entity-identifier-case)
4748
6. [Writable Entity Identifier](core/serialization-groups-and-relations.md#writable-entity-identifier)
4849
7. [Embedding the Context](core/serialization-groups-and-relations.md#embedding-the-context)
49-
7. [Validation](core/validation.md)
50+
8. [Validation](core/validation.md)
5051
1. [Using Validation Groups](core/validation.md#using-validation-groups)
5152
2. [Dynamic Validation Groups](core/validation.md#dynamic-validation-groups)
52-
8. [Pagination](core/pagination.md)
53+
9. [Pagination](core/pagination.md)
5354
1. [Disabling the Pagination](core/pagination.md#disabling-the-pagination)
5455
2. [Changing the Number of Items per Page](core/pagination.md#changing-the-number-of-items-per-page)
55-
9. [The Event System](core/events.md)
56-
10. [Content Negotiation](core/content-negotiation.md)
56+
10. [The Event System](core/events.md)
57+
11. [Content Negotiation](core/content-negotiation.md)
5758
1. [Enabling Several Formats](core/content-negotiation.md#enabling-several-formats)
5859
2. [Registering a Custom Serializer](core/content-negotiation.md#registering-a-custom-serializer)
5960
3. [Creating a Responder](core/content-negotiation.md#creating-a-responder)
6061
4. [Writing a Custom Normalizer](core/content-negotiation.md#writing-a-custom-normalizer)
61-
11. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
62-
12. [Extending JSON-LD context](core/extending-jsonld-context.md)
63-
13. [Data Providers](core/data-providers.md)
62+
12. [Using External JSON-LD Vocabularies](core/external-vocabularies.md)
63+
13. [Extending JSON-LD context](core/extending-jsonld-context.md)
64+
14. [Data Providers](core/data-providers.md)
6465
1. [Custom Collection Data Provider](core/data-providers.md#creating-a-custom-data-provider#custom-collection-data-provider)
6566
2. [Custom Item Data Provider](core/data-providers.md#returning-a-paged-collection#custom-item-data-provider)
66-
14. [Extensions](core/extensions.md)
67+
15. [Extensions](core/extensions.md)
6768
1. [Custom Extension](core/extensions.md#custom-extension)
6869
2. [Filter upon the current user](core/extensions.md#example)
6970
16. [Security](core/security.md)

0 commit comments

Comments
 (0)