From 5ef80b362ef36da526a8f3cfaa695649ba7f7089 Mon Sep 17 00:00:00 2001 From: Matthieu Date: Mon, 9 Feb 2026 15:58:43 +0100 Subject: [PATCH] perf(api) : add serialization groups to CustomFieldValue and CustomField Expose customField definitions (id, name, type, required, options, orderIndex) inline in entity responses, eliminating separate API calls for custom field data. Co-Authored-By: Claude Opus 4.6 --- src/Entity/CustomField.php | 32 ++++++++++++++++++++------------ src/Entity/CustomFieldValue.php | 31 +++++++++++++++++++------------ 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/Entity/CustomField.php b/src/Entity/CustomField.php index c8da7e1..eb33089 100644 --- a/src/Entity/CustomField.php +++ b/src/Entity/CustomField.php @@ -6,10 +6,12 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use App\Repository\CustomFieldRepository; +use DateTimeImmutable; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Attribute\Groups; #[ORM\Entity(repositoryClass: CustomFieldRepository::class)] #[ORM\Table(name: 'custom_fields')] @@ -19,24 +21,30 @@ class CustomField { #[ORM\Id] #[ORM\Column(type: Types::STRING, length: 36)] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private ?string $id = null; #[ORM\Column(type: Types::STRING, length: 255)] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private string $name; #[ORM\Column(type: Types::STRING, length: 50)] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private string $type; #[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private bool $required = false; #[ORM\Column(type: Types::STRING, length: 255, nullable: true, name: 'defaultValue')] private ?string $defaultValue = null; #[ORM\Column(type: Types::JSON, nullable: true)] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private ?array $options = null; #[ORM\Column(type: Types::INTEGER, options: ['default' => 0], name: 'orderIndex')] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private int $orderIndex = 0; #[ORM\ManyToOne(targetEntity: TypeMachine::class, inversedBy: 'customFields')] @@ -62,10 +70,10 @@ class CustomField private Collection $customFieldValues; #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'createdAt')] - private \DateTimeImmutable $createdAt; + private DateTimeImmutable $createdAt; #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'updatedAt')] - private \DateTimeImmutable $updatedAt; + private DateTimeImmutable $updatedAt; public function __construct() { @@ -75,11 +83,11 @@ class CustomField #[ORM\PrePersist] public function setCreatedAtValue(): void { - $now = new \DateTimeImmutable(); + $now = new DateTimeImmutable(); $this->createdAt = $now; $this->updatedAt = $now; - if ($this->id === null) { + if (null === $this->id) { $this->id = $this->generateCuid(); } } @@ -87,12 +95,7 @@ class CustomField #[ORM\PreUpdate] public function setUpdatedAtValue(): void { - $this->updatedAt = new \DateTimeImmutable(); - } - - private function generateCuid(): string - { - return 'cl' . bin2hex(random_bytes(12)); + $this->updatedAt = new DateTimeImmutable(); } public function getId(): ?string @@ -191,13 +194,18 @@ class CustomField return $this; } - public function getCreatedAt(): \DateTimeImmutable + public function getCreatedAt(): DateTimeImmutable { return $this->createdAt; } - public function getUpdatedAt(): \DateTimeImmutable + public function getUpdatedAt(): DateTimeImmutable { return $this->updatedAt; } + + private function generateCuid(): string + { + return 'cl'.bin2hex(random_bytes(12)); + } } diff --git a/src/Entity/CustomFieldValue.php b/src/Entity/CustomFieldValue.php index 1f30f51..36a5514 100644 --- a/src/Entity/CustomFieldValue.php +++ b/src/Entity/CustomFieldValue.php @@ -6,8 +6,10 @@ namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use App\Repository\CustomFieldValueRepository; +use DateTimeImmutable; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Serializer\Attribute\Groups; #[ORM\Entity(repositoryClass: CustomFieldValueRepository::class)] #[ORM\Table(name: 'custom_field_values')] @@ -17,13 +19,16 @@ class CustomFieldValue { #[ORM\Id] #[ORM\Column(type: Types::STRING, length: 36)] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private ?string $id = null; #[ORM\Column(type: Types::STRING, length: 255)] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private string $value; #[ORM\ManyToOne(targetEntity: CustomField::class, inversedBy: 'customFieldValues')] #[ORM\JoinColumn(name: 'customFieldId', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] private CustomField $customField; #[ORM\ManyToOne(targetEntity: Machine::class, inversedBy: 'customFieldValues')] @@ -43,19 +48,21 @@ class CustomFieldValue private ?Product $product = null; #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'createdAt')] - private \DateTimeImmutable $createdAt; + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] + private DateTimeImmutable $createdAt; #[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'updatedAt')] - private \DateTimeImmutable $updatedAt; + #[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])] + private DateTimeImmutable $updatedAt; #[ORM\PrePersist] public function setCreatedAtValue(): void { - $now = new \DateTimeImmutable(); + $now = new DateTimeImmutable(); $this->createdAt = $now; $this->updatedAt = $now; - if ($this->id === null) { + if (null === $this->id) { $this->id = $this->generateCuid(); } } @@ -63,12 +70,7 @@ class CustomFieldValue #[ORM\PreUpdate] public function setUpdatedAtValue(): void { - $this->updatedAt = new \DateTimeImmutable(); - } - - private function generateCuid(): string - { - return 'cl' . bin2hex(random_bytes(12)); + $this->updatedAt = new DateTimeImmutable(); } public function getId(): ?string @@ -155,13 +157,18 @@ class CustomFieldValue return $this; } - public function getCreatedAt(): \DateTimeImmutable + public function getCreatedAt(): DateTimeImmutable { return $this->createdAt; } - public function getUpdatedAt(): \DateTimeImmutable + public function getUpdatedAt(): DateTimeImmutable { return $this->updatedAt; } + + private function generateCuid(): string + { + return 'cl'.bin2hex(random_bytes(12)); + } }