136 lines
2.9 KiB
PHP
136 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Entity;
|
|
|
|
use ApiPlatform\Metadata\ApiProperty;
|
|
use ApiPlatform\Metadata\ApiResource;
|
|
use ApiPlatform\Metadata\Get;
|
|
use ApiPlatform\Metadata\GetCollection;
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
use Symfony\Component\Serializer\Attribute\Groups;
|
|
|
|
#[ORM\Entity]
|
|
#[ORM\Table(name: 'vehicle')]
|
|
#[ApiResource(
|
|
operations: [
|
|
new Get(
|
|
requirements: ['id' => '\d+'],
|
|
normalizationContext: ['groups' => ['vehicle:read']],
|
|
),
|
|
new GetCollection(
|
|
normalizationContext: ['groups' => ['vehicle:read']],
|
|
),
|
|
],
|
|
security: "is_granted('ROLE_USER')",
|
|
)]
|
|
class Vehicle
|
|
{
|
|
#[ORM\Id]
|
|
#[ORM\GeneratedValue]
|
|
#[ORM\Column]
|
|
#[Groups(['vehicle:read'])]
|
|
private ?int $id = null;
|
|
|
|
#[ORM\Column(length: 20)]
|
|
#[Groups(['vehicle:read'])]
|
|
private string $plate = '';
|
|
|
|
#[ORM\ManyToOne]
|
|
#[ORM\JoinColumn(nullable: false)]
|
|
#[Groups(['vehicle:read'])]
|
|
#[ApiProperty(readableLink: true)]
|
|
private ?Carrier $carrier = null;
|
|
|
|
#[ORM\ManyToOne]
|
|
#[ORM\JoinColumn(nullable: false)]
|
|
#[Groups(['vehicle:read'])]
|
|
#[ApiProperty(readableLink: true)]
|
|
private ?Truck $truck = null;
|
|
|
|
/**
|
|
* @var Collection<int, Shipment>
|
|
*/
|
|
#[ORM\OneToMany(targetEntity: Shipment::class, mappedBy: 'id_carrier_1')]
|
|
private Collection $shipments;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->shipments = new ArrayCollection();
|
|
}
|
|
|
|
public function getId(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getPlate(): string
|
|
{
|
|
return $this->plate;
|
|
}
|
|
|
|
public function setPlate(string $plate): self
|
|
{
|
|
$this->plate = $plate;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getCarrier(): ?Carrier
|
|
{
|
|
return $this->carrier;
|
|
}
|
|
|
|
public function setCarrier(?Carrier $carrier): self
|
|
{
|
|
$this->carrier = $carrier;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getTruck(): ?Truck
|
|
{
|
|
return $this->truck;
|
|
}
|
|
|
|
public function setTruck(?Truck $truck): self
|
|
{
|
|
$this->truck = $truck;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Shipment>
|
|
*/
|
|
public function getShipments(): Collection
|
|
{
|
|
return $this->shipments;
|
|
}
|
|
|
|
public function addShipment(Shipment $shipment): static
|
|
{
|
|
if (!$this->shipments->contains($shipment)) {
|
|
$this->shipments->add($shipment);
|
|
$shipment->setIdCarrier1($this);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeShipment(Shipment $shipment): static
|
|
{
|
|
if ($this->shipments->removeElement($shipment)) {
|
|
// set the owning side to null (unless already changed)
|
|
if ($shipment->getIdCarrier1() === $this) {
|
|
$shipment->setIdCarrier1(null);
|
|
}
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
}
|