src/Entity/City.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CityRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.  * @ORM\Entity(repositoryClass=CityRepository::class)
  9.  * @ORM\Table(name="city")
  10.  * @ORM\HasLifecycleCallbacks()
  11.  */
  12. class City
  13. {
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\GeneratedValue
  17.      * @ORM\Column(type="integer")
  18.      */
  19.     private ?int $id null;
  20.     /**
  21.      * @ORM\ManyToOne(targetEntity=Country::class, inversedBy="cities")
  22.      * @ORM\JoinColumn(nullable=false)
  23.      */
  24.     private Country $country;
  25.     /**
  26.      * @ORM\Column(type="string", length=100)
  27.      */
  28.     private string $name;
  29.     /**
  30.      * @ORM\Column(type="string", length=120, unique=true)
  31.      */
  32.     private string $slug;
  33.     /**
  34.      * @ORM\Column(type="boolean", options={"default": true})
  35.      */
  36.     private bool $isActive true;
  37.     /**
  38.      * @ORM\Column(type="datetime_immutable")
  39.      */
  40.     private \DateTimeImmutable $createdAt;
  41.     /**
  42.      * @ORM\OneToMany(targetEntity=Location::class, mappedBy="city", cascade={"persist"}, orphanRemoval=true)
  43.      */
  44.     private Collection $locations;
  45.     public function __construct()
  46.     {
  47.         $this->locations = new ArrayCollection();
  48.         $this->createdAt = new \DateTimeImmutable();
  49.     }
  50.     public function getId(): ?int
  51.     {
  52.         return $this->id;
  53.     }
  54.     public function getCountry(): Country
  55.     {
  56.         return $this->country;
  57.     }
  58.     public function setCountry(?Country $country): self
  59.     {
  60.         $this->country $country;
  61.         return $this;
  62.     }
  63.     public function getName(): string
  64.     {
  65.         return $this->name;
  66.     }
  67.     public function setName(string $name): self
  68.     {
  69.         $this->name $name;
  70.         return $this;
  71.     }
  72.     public function getSlug(): string
  73.     {
  74.         return $this->slug;
  75.     }
  76.     public function setSlug(string $slug): self
  77.     {
  78.         $this->slug $slug;
  79.         return $this;
  80.     }
  81.     public function isActive(): bool
  82.     {
  83.         return $this->isActive;
  84.     }
  85.     public function setIsActive(bool $isActive): self
  86.     {
  87.         $this->isActive $isActive;
  88.         return $this;
  89.     }
  90.     public function getCreatedAt(): \DateTimeImmutable
  91.     {
  92.         return $this->createdAt;
  93.     }
  94.     public function getLocations(): Collection
  95.     {
  96.         return $this->locations;
  97.     }
  98.     public function addLocation(Location $location): self
  99.     {
  100.         if (!$this->locations->contains($location)) {
  101.             $this->locations->add($location);
  102.             $location->setCity($this);
  103.         }
  104.         return $this;
  105.     }
  106.     public function removeLocation(Location $location): self
  107.     {
  108.         if ($this->locations->removeElement($location)) {
  109.             if ($location->getCity() === $this) {
  110.                 $location->setCity(null);
  111.             }
  112.         }
  113.         return $this;
  114.     }
  115.     public function __toString(): string
  116.     {
  117.         return ($this->title ?? '');
  118.     }
  119. }