src/Entity/ResourceCategory.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Interfaces\DefaultEntity;
  4. use App\Entity\Traits\ActiveTrait;
  5. use App\Entity\Traits\DeleteTrait;
  6. use App\Entity\Traits\SortOrderTrait;
  7. use App\Entity\Traits\TitleAndContentTrait;
  8. use App\Entity\Traits\TranslateTrait;
  9. use App\Repository\ResourceCategoryRepository;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Gedmo\Mapping\Annotation as Gedmo;
  14. use Gedmo\Timestampable\Traits\TimestampableEntity;
  15. use Doctrine\Common\Collections\Criteria;
  16. /**
  17. * @Gedmo\Loggable
  18. */
  19. #[ORM\Entity(repositoryClass: ResourceCategoryRepository::class)]
  20. #[ORM\HasLifecycleCallbacks]
  21. #[ORM\Table(name: 'resource_category')]
  22. class ResourceCategory implements DefaultEntity
  23. {
  24. use TitleAndContentTrait;
  25. use SortOrderTrait;
  26. use ActiveTrait;
  27. use DeleteTrait;
  28. use TimestampableEntity;
  29. use TranslateTrait;
  30. #[ORM\Column(name: 'id', type: 'integer')]
  31. #[ORM\Id]
  32. #[ORM\GeneratedValue(strategy: 'AUTO')]
  33. private readonly int $id;
  34. #[ORM\OneToMany(mappedBy: 'category', targetEntity: Resource::class)]
  35. private $resources;
  36. public function __construct()
  37. {
  38. $this->resources = new ArrayCollection();
  39. }
  40. public function getId(): ?int
  41. {
  42. return $this->id;
  43. }
  44. /**
  45. * @return Collection<int, Resource>
  46. */
  47. public function getResources(): Collection
  48. {
  49. return $this->resources;
  50. }
  51. public function getActiveResources(): Collection
  52. {
  53. $criteria = Criteria::create()
  54. ->andWhere(Criteria::expr()->eq('active', true))
  55. ->andWhere(Criteria::expr()->eq('deleted', false))
  56. ->orderBy(['sortOrder' => 'ASC'])
  57. ;
  58. return $this->resources->matching($criteria);
  59. }
  60. public function addResource(Resource $resource): self
  61. {
  62. if (!$this->resources->contains($resource)) {
  63. $this->resources[] = $resource;
  64. $resource->setCategory($this);
  65. }
  66. return $this;
  67. }
  68. public function removeResource(Resource $resource): self
  69. {
  70. if ($this->resources->removeElement($resource)) {
  71. // set the owning side to null (unless already changed)
  72. if ($resource->getCategory() === $this) {
  73. $resource->setCategory(null);
  74. }
  75. }
  76. return $this;
  77. }
  78. }