Skip to content

docs: add missing yaml documentation in the default order section #1375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions core/default-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ API Platform Core provides an easy way to override the default order of items in
By default, items in the collection are ordered in ascending (ASC) order by their resource identifier(s). If you want to
customize this order, you must add an `order` attribute on your ApiResource annotation:

[codeSelector]

```php
<?php
// api/src/Entity/Book.php
Expand All @@ -28,9 +30,21 @@ class Book
}
```

```yaml
# config/api/resources/Book.yaml
App\Entity\Book:
attributes:
order:
foo: ASC
```

[/codeSelector]

This `order` attribute is used as an array: the key defines the order field, the values defines the direction.
If you only specify the key, `ASC` direction will be used as default. For example, to order by `foo` & `bar`:

[codeSelector]

```php
<?php
// api/src/Entity/Book.php
Expand Down Expand Up @@ -59,8 +73,19 @@ class Book
}
```

```yaml
# config/api/resources/Book.yaml
App\Entity\Book:
attributes:
order: ['foo', 'bar']
```

[/codeSelector]

It's also possible to configure the default order on an association property:

[codeSelector]

```php
<?php
// api/src/Entity/Book.php
Expand All @@ -84,8 +109,19 @@ class Book
}
```

```yaml
# config/api/resources/Book.yaml
App\Entity\Book:
attributes:
order: ['author.username']
```

[/codeSelector]

Another possibility is to apply the default order for a specific collection operation, which will override the global default order configuration.

[codeSelector]

```php
/**
* collectionOperations={
Expand All @@ -106,3 +142,21 @@ class Book
// ...
}
```

```yaml
# config/api/resources/Book.yaml
App\Entity\Book:
get: ~
get_desc_custom:
method: get
path: custom_collection_desc_foos
order:
name: DESC
get_asc_custom:
method: get
path: custom_collection_asc_foos
order:
name: ASC
```

[/codeSelector]