src/Entity/CourseCategory.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CourseCategoryRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Iterator;
  10. /**
  11.  * @ORM\Entity(repositoryClass=CourseCategoryRepository::class)
  12.  * @UniqueEntity(fields={"level"}, message="There is already a category with this level")
  13.  */
  14. class CourseCategory
  15. {
  16.     /**
  17.      * @ORM\Id
  18.      * @ORM\GeneratedValue
  19.      * @ORM\Column(type="integer")
  20.      */
  21.     private $id;
  22.     /**
  23.      * @ORM\Column(type="string", length=255)
  24.      */
  25.     private $name;
  26.     /**
  27.      * @ORM\ManyToOne(targetEntity=CourseCategory::class, inversedBy="courseCategories")
  28.      */
  29.     private $parent;
  30.     /**
  31.      * @ORM\OneToMany(targetEntity=CourseCategory::class, mappedBy="parent")
  32.      */
  33.     private $courseCategories;
  34.     /**
  35.      * @ORM\OneToMany(targetEntity=Cours::class, mappedBy="category", cascade={"remove"})
  36.      */
  37.     private $cours;
  38.     /**
  39.      * @ORM\Column(type="integer")
  40.      */
  41.     private $level;
  42.     public function __construct()
  43.     {
  44.         $this->courseCategories = new ArrayCollection();
  45.         $this->cours = new ArrayCollection();
  46.         $this->level 1;
  47.     }
  48.     public function getId(): ?int
  49.     {
  50.         return $this->id;
  51.     }
  52.     public function getName(): ?string
  53.     {
  54.         return $this->name;
  55.     }
  56.     public function setName(string $name): self
  57.     {
  58.         $this->name $name;
  59.         return $this;
  60.     }
  61.     public function getParent(): ?self
  62.     {
  63.         return $this->parent;
  64.     }
  65.     public function setParent(?self $parent): self
  66.     {
  67.         $this->parent $parent;
  68.         return $this;
  69.     }
  70.     /**
  71.      * @return Collection<int, self>
  72.      */
  73.     public function getCourseCategories(): Collection
  74.     {
  75.         return $this->courseCategories;
  76.     }
  77.     public function addCourseCategory(self $courseCategory): self
  78.     {
  79.         if (!$this->courseCategories->contains($courseCategory)) {
  80.             $this->courseCategories[] = $courseCategory;
  81.             $courseCategory->setParent($this);
  82.         }
  83.         return $this;
  84.     }
  85.     public function removeCourseCategory(self $courseCategory): self
  86.     {
  87.         if ($this->courseCategories->removeElement($courseCategory)) {
  88.             // set the owning side to null (unless already changed)
  89.             if ($courseCategory->getParent() === $this) {
  90.                 $courseCategory->setParent(null);
  91.             }
  92.         }
  93.         return $this;
  94.     }
  95.     /**
  96.      * @return Collection<int, Cours>
  97.      */
  98.     public function getCours(): Collection
  99.     {
  100.         return $this->cours;
  101.     }
  102.     public function addCour(Cours $cour): self
  103.     {
  104.         if (!$this->cours->contains($cour)) {
  105.             $this->cours[] = $cour;
  106.             $cour->setCategory($this);
  107.         }
  108.         return $this;
  109.     }
  110.     public function removeCour(Cours $cour): self
  111.     {
  112.         if ($this->cours->removeElement($cour)) {
  113.             // set the owning side to null (unless already changed)
  114.             if ($cour->getCategory() === $this) {
  115.                 $cour->setCategory(null);
  116.             }
  117.         }
  118.         return $this;
  119.     }
  120.     public function getPublishedCourse(): Collection
  121.     {
  122.         $criteria Criteria::create()
  123.             ->where(Criteria::expr()->eq('published'true))
  124.             ->orderBy(['id' => 'ASC']);
  125.         
  126.         return $this->cours->matching($criteria);
  127.     }
  128.     public function __toString()
  129.     {
  130.         return $this->name;
  131.     }
  132.     public function getLevel(): ?int
  133.     {
  134.         return $this->level;
  135.     }
  136.     public function setLevel(int $level): self
  137.     {
  138.         $this->level $level;
  139.         return $this;
  140.     }
  141. }