Skip to content

Instantly share code, notes, and snippets.

@mmouih
Created April 7, 2024 18:05
Show Gist options
  • Save mmouih/b0524d76c73afd176c0a75cd94da4fce to your computer and use it in GitHub Desktop.
Save mmouih/b0524d76c73afd176c0a75cd94da4fce to your computer and use it in GitHub Desktop.
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\Table(name: "invoice")]
class Invoice
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: "integer")]
private int $id;
#[ORM\Column(type: "string", length: 255)]
private string $invoiceNumber;
#[ORM\Column(type: "decimal", precision: 10, scale: 2)]
private string $amount;
#[ORM\Column(type: "datetime")]
private \DateTimeInterface $createdAt;
// Constructor
public function __construct(string $invoiceNumber, string $amount)
{
$this->invoiceNumber = $invoiceNumber;
$this->amount = $amount;
$this->createdAt = new \DateTimeImmutable();
}
// ...
}
<?php
// ...
use App\Entity\Invoice;
use Symfony\Component\Serializer\SerializerInterface;
#[Route('/invoices')]
class InvoiceController extends AbstractController
{
#[Route('/{id}', name: 'invoice_show', methods: ['GET'])]
public function show(Invoice $invoice, SerializerInterface $serializer): JsonResponse
{
$jsonData = $serializer->serialize($invoice, 'json');
return new JsonResponse($jsonData, 200, [], true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment