0

I'm trying to set up an ApiRessource using API Platform.
Here is my Cheese entity:

#[ORM\Entity(repositoryClass: CheeseListingRepository::class)]
#[ApiResource]
class CheeseListing
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $title = null;


    #[ORM\Column(type: Types::TEXT)]
    private ?string $description = null;

    #[ORM\Column]
    private ?int $price = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\Column]
    private ?bool $isPublished = null;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): self
    {
        $this->description = $description;

        return $this;
    }

    public function setTextDescription(string $description): self
    {
        $this->description = nl2br($description);

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function getCreatedAtAgo(): string
    {
        return Carbon::instance($this->getCreatedAt())->diffForHumans();
    }

    public function setCreatedAt(\DateTimeImmutable $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }
}

While my custom getter makes the field createdAtAgo appears in the JSON returned by GET operations:

{
    "id": 0,
    "title": "string",
    "description": "string",
    "createdAt": "2022-08-26T00:44:53.226Z",
    "createdAtAgo": "string"
 }

I'm not finding a way to be able to pass the textDescription to the POST opérations although I defined the setter setTextDescription. Here is the example value the doc keeps showing to me:

{
  "title": "string",
  "description": "string",
  "createdAt": "2022-08-26T00:55:41.558Z"
}

Any idea to help me add the textDescription field?

Nel
  • 454
  • 5
  • 14

1 Answers1

0

I started a brand new project and I'm getting the expected result now.

Nel
  • 454
  • 5
  • 14