Performance

To make the admin faster and greener, you can make some changes to your API.

管理をより迅速かつ環境に配慮したものにするために、API にいくつかの変更を加えることができます。

Retrieve All Relations in One Request

By default, if your relations are not embedded and if you decide to display some fields belonging to relations in your resource list, the admin will fetch the relations one by one.

デフォルトでは、リレーションが埋め込まれておらず、リレーションに属するいくつかのフィールドをリソース リストに表示することにした場合、管理者はリレーションを 1 つずつ取得します。

In this case, it can be improved by doing only one request for all the related resources instead.

この場合、代わりにすべての関連リソースに対して 1 つのリクエストのみを実行することで改善できます。

To do so, you need to make sure the search filter is enabled for the identifier of the related resource.

これを行うには、関連するリソースの識別子に対して検索フィルターが有効になっていることを確認する必要があります。

For instance, if you have a book resource having a relation to author resources and you display the author names on your book list, you can make sure the authors are retrieved in one go by writing:

たとえば、著者のリソースに関連する書籍のリソースがあり、書籍リストに著者名を表示する場合、次のように記述して著者を一度に取得することができます。

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

use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiResource]
class Author
{
    #[ORM\Id, ORM\Column, ORM\GeneratedValue]
    #[ApiFilter(SearchFilter::class, strategy: "exact")]
    public ?int $id = null;

    #[ORM\Column]
    public string $name;
}