I have a Doctrine Entity which I want to map into an Api Platform resource:
/**
* @ApiResource
* @ORM\Entity
*/
class Book
{
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
*/
private UuidInterface $uuid;
/**
* @var string
* @ORM\Column
* @Assert\NotBlank
*/
private string $title;
/**
* @ORM\Column(type="datetime")
*/
private DateTimeInterface $createdAt;
public function __construct(UuidInterface $uuid, string $title, DateTimeInterface $createdAt)
{
$this->uuid = $uuid;
$this->title = $title;
$this->createdAt = $createdAt;
}
public function getUuid(): string
{
return $this->uuid->toString();
}
public function getTitle(): string
{
return $this->title;
}
public function getCreatedAt(): string
{
return $this->createdAt->format('Y-m-d H:i:s');
}
}
It is properly shown on the corresponding resource of my api:
What I'm trying to do now is to use a custom getter name without get prefix for all attributes:
public function uuid(): string;
public function title(): string;
public function createdAt(): string;
But then the fields are not shown on my API platform page. How can I configure API Platform/Doctrine/Symfony or whatever so I can use custom names for my getters?
By changing these method names, Symfony's property access won't recognize them as actual getters, and will be ingored by the serializer, it is possible to expose them using serialization groups.
However, your methods wont be treated as getters, so it may cause some weirdness with the serializer or Api platform. This is a standard convention it's best to not change them. Behind the scenes, this is handled by Symfony's property access component.