list products with DTO

This commit is contained in:
Tykayn 2025-02-17 16:34:21 +01:00 committed by tykayn
parent cd4b678002
commit 0d672d5447
7 changed files with 57 additions and 11 deletions

View file

@ -61,10 +61,21 @@ class GroupOfProducts
return $this;
}
public function getProducts(): ?object
/**
* @return array
*/
public function getProductsDTO(): array
{
return $this->products;
$productsDTO = [];
foreach ($this->products as $product) {
$productsDTO[] = $product->toArray();
}
return $productsDTO;
}
public function getProducts(): ?object
{
return $this->products;
}
public function setProducts(object $products): static
{

View file

@ -11,6 +11,7 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Filter\UserProductsFilter;
use App\DTO\UserDTO;
use Symfony\Component\Serializer\Annotation\Groups;
#[ApiResource(paginationEnabled: false)]
#[UserProductsFilter]
@ -23,9 +24,11 @@ class Product
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Groups(['default', 'relation'])]
private ?string $name = null;
#[ORM\Column]
#[Groups(['default', 'relation'])]
private ?float $price = null;
#[ORM\Column]
@ -190,4 +193,14 @@ class Product
return $this;
}
public function toArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'price' => $this->price,
// Ajoutez d'autres propriétés si nécessaire
];
}
}