<?php
namespace App\Entity;
use App\Repository\RegionRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: RegionRepository::class)]
#[ORM\Table(name: 'region')]
class Region
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Country::class, inversedBy: 'regions')]
#[ORM\JoinColumn(name: 'country_id', referencedColumnName: 'id', nullable: false)]
private ?Country $country = null;
#[ORM\Column(length: 100)]
private ?string $name = null;
#[ORM\Column(length: 50)]
private ?string $code = null;
/** @var Collection<int, City> */
#[ORM\OneToMany(mappedBy: 'region', targetEntity: City::class, cascade: ['persist'])]
private Collection $cities;
public function __construct()
{
$this->cities = new ArrayCollection();
}
public function getId(): ?int { return $this->id; }
public function getCountry(): ?Country { return $this->country; }
public function setCountry(?Country $country): self { $this->country = $country; return $this; }
public function getName(): ?string { return $this->name; }
public function setName(string $name): self { $this->name = $name; return $this; }
public function getCode(): ?string { return $this->code; }
public function setCode(string $code): self { $this->code = $code; return $this; }
/** @return Collection<int, City> */
public function getCities(): Collection { return $this->cities; }
public function addCity(City $city): self
{
if (!$this->cities->contains($city)) {
$this->cities->add($city);
$city->setRegion($this);
}
return $this;
}
public function removeCity(City $city): self
{
if ($this->cities->removeElement($city)) {
if ($city->getRegion() === $this) {
$city->setRegion(null);
}
}
return $this;
}
public function __toString(): string { return $this->name ?? ''; }
}