src/Entity/Community.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use App\Repository\CommunityRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Knp\DoctrineBehaviors\Contract\Entity\SluggableInterface;
  9. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  10. use Knp\DoctrineBehaviors\Model\Sluggable\SluggableTrait;
  11. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  12. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  13. use Symfony\Component\HttpFoundation\File\File;
  14. /**
  15.  * @ORM\Entity(repositoryClass=CommunityRepository::class)
  16.   * @Vich\Uploadable
  17.  */
  18. class Community implements TranslatableInterfaceSluggableInterface
  19. {
  20.     use TranslatableTrait;
  21.     use SluggableTrait;
  22.     public function __toString()
  23.     {
  24.         if( $name $this->translate()->getName()) {
  25.             return $name;
  26.         }
  27.         // if no translation has been added, return empty string instead.
  28.         return '';
  29.     }
  30.     /**
  31.      * @ORM\OneToMany(targetEntity="App\Entity\Masjid", mappedBy="community")
  32.      * @ORM\OrderBy({"name" = "ASC"})
  33.      */
  34.     private $masjids;
  35.     /**
  36.      * @ORM\Id
  37.      * @ORM\GeneratedValue
  38.      * @ORM\Column(type="integer")
  39.      */
  40.     private $id;
  41.     /**
  42.      * @ORM\ManyToOne(targetEntity=Emirate::class)
  43.      * @ORM\JoinColumn(nullable=false)
  44.      */
  45.     private $emirate;
  46.     /**
  47.      * @ORM\Column(type="string", length=255, nullable=true)
  48.      */
  49.     private $mainPic;
  50.      /**
  51.      * NOTE: This is not a mapped field of entity metadata, just a simple property.
  52.      *
  53.      * @Vich\UploadableField(mapping="community_mainPic", fileNameProperty="mainPic")
  54.      *
  55.      * @var File|null
  56.      */
  57.     private $imageFile;
  58.     /**
  59.      * @ORM\Column(type="datetime")
  60.      *
  61.      * @var \DateTimeInterface|null
  62.      */
  63.     private $updatedAt;
  64.     /**
  65.      * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
  66.      * of 'UploadedFile' is injected into this setter to trigger the update. If this
  67.      * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
  68.      * must be able to accept an instance of 'File' as the bundle will inject one here
  69.      * during Doctrine hydration.
  70.      *
  71.      * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
  72.      */
  73.     public function setImageFile(File $image null)
  74.     {
  75.         $this->imageFile $image;
  76.         // VERY IMPORTANT:
  77.         // It is required that at least one field changes if you are using Doctrine,
  78.         // otherwise the event listeners won't be called and the file is lost
  79.         if ($image) {
  80.             // if 'updatedAt' is not defined in your entity, use another property
  81.             $this->updatedAt = new \DateTime('now');
  82.         }
  83.     }
  84.     public function getImageFile(): ?File
  85.     {
  86.         return $this->imageFile;
  87.     }
  88.     public function __construct()
  89.     {
  90.         $this->masjids = new ArrayCollection();
  91.     }
  92.     public function getId(): ?int
  93.     {
  94.         return $this->id;
  95.     }
  96.     public function getEmirate(): ?Emirate
  97.     {
  98.         return $this->emirate;
  99.     }
  100.     public function setEmirate(?Emirate $emirate): self
  101.     {
  102.         $this->emirate $emirate;
  103.         return $this;
  104.     }
  105.     /**
  106.      * @return Collection|Masjid[]
  107.      */
  108.     public function getMasjids(): Collection
  109.     {
  110.         return $this->masjids;
  111.     }
  112.     public function addMasjid(Masjid $masjid): self
  113.     {
  114.         if (!$this->masjids->contains($masjid)) {
  115.             $this->masjids[] = $masjid;
  116.             $masjid->setCommunity($this);
  117.         }
  118.         return $this;
  119.     }
  120.     public function removeMasjid(Masjid $masjid): self
  121.     {
  122.         if ($this->masjids->removeElement($masjid)) {
  123.             // set the owning side to null (unless already changed)
  124.             if ($masjid->getCommunity() === $this) {
  125.                 $masjid->setCommunity(null);
  126.             }
  127.         }
  128.         return $this;
  129.     }
  130.     /**
  131.      * @return string[]
  132.      */
  133.     public function getSluggableFields(): array
  134.     {
  135.         return ["name"];
  136.     }
  137.     public function generateSlugValue($values): string
  138.     {
  139.         return implode('-'$values);
  140.     }
  141.     public function shouldRegenerateSlugOnUpdate(): bool{
  142.         return false;
  143.     }
  144.     public function getMainPic(): ?string
  145.     {
  146.         return $this->mainPic;
  147.     }
  148.     public function setMainPic(?string $mainPic): self
  149.     {
  150.         $this->mainPic $mainPic;
  151.         return $this;
  152.     }
  153. }