The path field (base64 data URIs) is now excluded from GetCollection via document:list group. Individual GET returns path via document:detail group. Related entities expose id+name in document:list for attachment display. Frontend lazy-loads path on download/preview click. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
257 lines
6.4 KiB
PHP
257 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Entity;
|
|
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
use ApiPlatform\Metadata\Delete;
|
|
use ApiPlatform\Metadata\Get;
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
use ApiPlatform\Metadata\Post;
|
|
use ApiPlatform\Metadata\Put;
|
|
use App\Repository\DocumentRepository;
|
|
use DateTimeImmutable;
|
|
use Doctrine\DBAL\Types\Types;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Serializer\Attribute\Groups;
|
|
|
|
#[ORM\Entity(repositoryClass: DocumentRepository::class)]
|
|
#[ORM\Table(name: 'documents')]
|
|
#[ORM\HasLifecycleCallbacks]
|
|
#[ApiResource(
|
|
operations: [
|
|
new GetCollection(normalizationContext: ['groups' => ['document:list']]),
|
|
new Get(normalizationContext: ['groups' => ['document:list', 'document:detail']]),
|
|
new Post(),
|
|
new Put(),
|
|
new Delete(),
|
|
],
|
|
paginationClientItemsPerPage: true,
|
|
paginationMaximumItemsPerPage: 200
|
|
)]
|
|
class Document
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\Column(type: Types::STRING, length: 36)]
|
|
#[Groups(['document:list', 'document:read', 'composant:read', 'piece:read', 'product:read'])]
|
|
private ?string $id = null;
|
|
|
|
#[ORM\Column(type: Types::STRING, length: 255)]
|
|
#[Groups(['document:list', 'document:read', 'composant:read', 'piece:read', 'product:read'])]
|
|
private string $name;
|
|
|
|
#[ORM\Column(type: Types::STRING, length: 255)]
|
|
#[Groups(['document:list', 'document:read', 'composant:read', 'piece:read', 'product:read'])]
|
|
private string $filename;
|
|
|
|
#[ORM\Column(type: Types::TEXT)]
|
|
#[Groups(['document:detail', 'document:read', 'composant:read', 'piece:read', 'product:read'])]
|
|
private string $path;
|
|
|
|
#[ORM\Column(type: Types::STRING, length: 100, name: 'mimeType')]
|
|
#[Groups(['document:list', 'document:read', 'composant:read', 'piece:read', 'product:read'])]
|
|
private string $mimeType;
|
|
|
|
#[ORM\Column(type: Types::INTEGER)]
|
|
#[Groups(['document:list', 'document:read', 'composant:read', 'piece:read', 'product:read'])]
|
|
private int $size;
|
|
|
|
#[ORM\ManyToOne(targetEntity: Machine::class, inversedBy: 'documents')]
|
|
#[ORM\JoinColumn(name: 'machineId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
|
|
#[Groups(['document:list'])]
|
|
private ?Machine $machine = null;
|
|
|
|
#[ORM\ManyToOne(targetEntity: Composant::class, inversedBy: 'documents')]
|
|
#[ORM\JoinColumn(name: 'composantId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
|
|
#[Groups(['document:list'])]
|
|
private ?Composant $composant = null;
|
|
|
|
#[ORM\ManyToOne(targetEntity: Piece::class, inversedBy: 'documents')]
|
|
#[ORM\JoinColumn(name: 'pieceId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
|
|
#[Groups(['document:list'])]
|
|
private ?Piece $piece = null;
|
|
|
|
#[ORM\ManyToOne(targetEntity: Product::class, inversedBy: 'documents')]
|
|
#[ORM\JoinColumn(name: 'productId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
|
|
#[Groups(['document:list'])]
|
|
private ?Product $product = null;
|
|
|
|
#[ORM\ManyToOne(targetEntity: Site::class, inversedBy: 'documents')]
|
|
#[ORM\JoinColumn(name: 'siteId', referencedColumnName: 'id', nullable: true, onDelete: 'CASCADE')]
|
|
#[Groups(['document:list'])]
|
|
private ?Site $site = null;
|
|
|
|
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'createdAt')]
|
|
#[Groups(['document:list'])]
|
|
private DateTimeImmutable $createdAt;
|
|
|
|
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, name: 'updatedAt')]
|
|
private DateTimeImmutable $updatedAt;
|
|
|
|
#[ORM\PrePersist]
|
|
public function setCreatedAtValue(): void
|
|
{
|
|
$now = new DateTimeImmutable();
|
|
$this->createdAt = $now;
|
|
$this->updatedAt = $now;
|
|
|
|
if (null === $this->id) {
|
|
$this->id = $this->generateCuid();
|
|
}
|
|
}
|
|
|
|
#[ORM\PreUpdate]
|
|
public function setUpdatedAtValue(): void
|
|
{
|
|
$this->updatedAt = new DateTimeImmutable();
|
|
}
|
|
|
|
public function getId(): ?string
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function setId(string $id): static
|
|
{
|
|
$this->id = $id;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getName(): string
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function setName(string $name): static
|
|
{
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getFilename(): string
|
|
{
|
|
return $this->filename;
|
|
}
|
|
|
|
public function setFilename(string $filename): static
|
|
{
|
|
$this->filename = $filename;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getPath(): string
|
|
{
|
|
return $this->path;
|
|
}
|
|
|
|
public function setPath(string $path): static
|
|
{
|
|
$this->path = $path;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getMimeType(): string
|
|
{
|
|
return $this->mimeType;
|
|
}
|
|
|
|
public function setMimeType(string $mimeType): static
|
|
{
|
|
$this->mimeType = $mimeType;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getSize(): int
|
|
{
|
|
return $this->size;
|
|
}
|
|
|
|
public function setSize(int $size): static
|
|
{
|
|
$this->size = $size;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getMachine(): ?Machine
|
|
{
|
|
return $this->machine;
|
|
}
|
|
|
|
public function setMachine(?Machine $machine): static
|
|
{
|
|
$this->machine = $machine;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getComposant(): ?Composant
|
|
{
|
|
return $this->composant;
|
|
}
|
|
|
|
public function setComposant(?Composant $composant): static
|
|
{
|
|
$this->composant = $composant;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getPiece(): ?Piece
|
|
{
|
|
return $this->piece;
|
|
}
|
|
|
|
public function setPiece(?Piece $piece): static
|
|
{
|
|
$this->piece = $piece;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getProduct(): ?Product
|
|
{
|
|
return $this->product;
|
|
}
|
|
|
|
public function setProduct(?Product $product): static
|
|
{
|
|
$this->product = $product;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getSite(): ?Site
|
|
{
|
|
return $this->site;
|
|
}
|
|
|
|
public function setSite(?Site $site): static
|
|
{
|
|
$this->site = $site;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getCreatedAt(): DateTimeImmutable
|
|
{
|
|
return $this->createdAt;
|
|
}
|
|
|
|
public function getUpdatedAt(): DateTimeImmutable
|
|
{
|
|
return $this->updatedAt;
|
|
}
|
|
|
|
private function generateCuid(): string
|
|
{
|
|
return 'cl'.bin2hex(random_bytes(12));
|
|
}
|
|
}
|