Overriding Default Order

API Platform provides an easy way to override the default order of items in your collection.

API プラットフォームでは、コレクション内のアイテムのデフォルトの順序を簡単にオーバーライドできます。

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:

デフォルトでは、コレクション内のアイテムはリソース識別子の昇順 (ASC) で並べられます。この順序をカスタマイズする場合は、ApiResource アノテーションに order 属性を追加する必要があります。

[codeSelector]

[コードセレクター]

<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource(order: ['foo' => 'ASC'])]
class Book
{
    // ...

    /**
     * ...
     */
    public $foo;

    // ...
}
# api/config/api_platform/resources/Book.yaml
App\Entity\Book:
    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
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource(order: ['foo', 'bar'])]
class Book
{
    // ...

    /**
     * ...
     */
    public $foo;

    /**
     * ...
     */
    public $bar;

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

[/codeSelector]

[/コードセレクター]

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

関連プロパティでデフォルトの順序を構成することもできます。

[codeSelector]

[コードセレクター]

<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;

#[ApiResource(order: ['author.username'])]
class Book
{
    // ...

    /**
     * @var User
     */
    public $author;

    // ...
}
# api/config/api_platform/resources/Book.yaml
App\Entity\Book:
    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.

もう 1 つの可能性は、特定のコレクション操作にデフォルトの順序を適用することです。これにより、グローバルなデフォルトの順序構成が上書きされます。

[codeSelector]

[コードセレクター]

<?php
// api/src/Entity/Book.php
namespace App\Entity;

use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\ApiResource;

#[ApiResource(operations: [
    new GetCollection(),
    new GetCollection(name: 'get_desc_custom', uriTemplate: 'custom_collection_desc_foos', order: ['name' => 'DESC'])],
    new GetCollection(name: 'get_asc_custom', uriTemplate: 'custom_collection_asc_foos', order: ['name' => 'ASC'])]
])]
class Book
{
    // ...

    /**
     * @var string
     */
    public $name;

    // ...
}
# api/config/api_platform/resources/Book.yaml
App\Entity\Book:
    ApiPlatform\Metadata\GetCollection: ~
    get_desc_custom:
        class: ApiPlatform\Metadata\GetCollection
        uriTemplate: custom_collection_desc_foos
        order:
            name: DESC
    get_asc_custom:
        class: ApiPlatform\Metadata\GetCollection
        uriTemplate: custom_collection_asc_foos
        order:
            name: ASC

[/codeSelector]

[/コードセレクター]