symfony
7.4
symfony/object-mapper
7.4
api-platform/symfony
4.2
php
8.4
Beschreiben Sie das Problem
Ich habe also über die neue Empfehlung für API Platform gelesen, die darin besteht, DTO zu verwenden und zuzuordnen, anstatt Metadaten von API Platform-Ressourcen direkt über die Entität zu schreiben.
Siehe auch https://api-platform.com/docs/core/dto/
Also habe ich es versucht Dies zu implementieren, ohne Erfolg.
Veranschaulichen Sie das Problem mit meinem Code
Hier ist meine einfache Entität:
Code: Select all
# src/Entity
namespace App\Entity;
#[ORM\Entity(repositoryClass: CompanyRepository::class)]
#[ORM\Table(name: 'company')]
class Company
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::BIGINT)]
#[Groups(['read:company'])]
private ?int $id = null;
#[ORM\Column(type: Types::STRING, length: 255)]
#[Groups(['read:company'])]
private string $name = '';
...
}
Code: Select all
# src/Domain/Company/ApiResource
namespace App\Domain\Company\ApiResource;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Post;
use App\Entity\Company;
use Symfony\Component\ObjectMapper\Attribute\Map;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource(
operations: [
new Post(
uriTemplate: '/company',
/** @todo security: 'is_granted("ROLE_ADMIN")', */
normalizationContext: ['groups' => ['read:company']],
output: CompanyCreateOutput::class,
name: 'create_company',
),
],
stateOptions: new Options(entityClass: Company::class),
)]
#[Map(target: Company::class)]
class CompanyCreateInput
{
public string $name = '';
}
Code: Select all
# src/Domain/Company/ApiResource
namespace App\Domain\Company\ApiResource;
use App\Entity\Company;
use Symfony\Component\ObjectMapper\Attribute\Map;
use Symfony\Component\Serializer\Attribute\Groups;
#[Map(source: Company::class)]
class CompanyCreateOutput
{
#[Groups(['read:company'])]
public int $id;
#[Groups(['read:company'])]
public string $name;
#[Groups(['read:company'])]
#[Map(source: 'created_at')]
public string $createdAt;
}
Code: Select all
POST {{url}}/company
{
"name": "This is a test"
}
Code: Select all
{
"@context": {
"@vocab": "https://api.project.local/api/docs.jsonld#",
"hydra": "http://www.w3.org/ns/hydra/core#",
"name": "CompanyCreateInput/name"
},
"@type": "CompanyCreateInput",
"@id": "/api/.well-known/genid/28ed6cfb3faf268d111c"
}
Was ich erreichen möchte
Ich möchte, dass beim Erstellen einer Ressource (mit einem POST) die erstellte Ressource zurückgegeben wird, z. B.:
Code: Select all
{
"id": 12345,
"name": This is a test",
"created_at": "29/12/2025 00:00:00"
}
- Ich habe gelesen, wie es gelungen ist, das Objekt auf die Standardmethode mithilfe von ItemNormalizer::normalize zu normalisieren, das zwar das Ausgabeargument einschließt, es aber nicht verwendet ... Für mich sieht es nach einem Fehler aus ...
- Ich habe versucht, einen benutzerdefinierten Anbieter zu erstellen CompanyCreateRepresentationProvider, aber ich wusste nicht, wie man es benutzt, und es sah nicht so aus, als ob es tatsächlich verwendet würde ...
Mobile version