src/Entity/Category.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Blameable\Traits\BlameableEntity;
  9. use Gedmo\Mapping\Annotation\Slug;
  10. use Gedmo\Timestampable\Traits\TimestampableEntity;
  11. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  12. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  15. #[ORM\MappedSuperclass()]
  16. #[UniqueEntity('slug')]
  17. #[ORM\HasLifecycleCallbacks]
  18. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  19. class Category implements TranslatableInterface
  20. {
  21.     use BlameableEntity//Hook blameable behaviour. Updates createdBy, updatedBy fields
  22.     use TimestampableEntity//Hook timestampable behaviour. Updates createdAt, updatedAt fields 
  23.     use TranslatableTrait;
  24.     
  25.     #[ORM\Id]
  26.     #[ORM\GeneratedValue(strategy"IDENTITY")]
  27.     #[ORM\Column]
  28.     private ?int $id null;
  29.     #[Assert\Valid]
  30.     protected $translations;
  31.     #[ORM\Column(length255uniquetrue)]
  32.     #[Slug(fields: ['slug'])]
  33.     private ?string $slug null;
  34.     #[ORM\ManyToMany(targetEntityProduct::class, mappedBy'categories')]
  35.     private Collection $products;
  36.     #[ORM\ManyToMany(targetEntityUser::class, mappedBy'categoryInterests')]
  37.     private Collection $users;
  38.     #[ORM\OneToOne(cascade: ['persist''remove'])]
  39.     private ?Media $icon null;
  40.     #[ORM\Column(nullabletrue)]
  41.     private ?bool $active null;
  42.     #[ORM\Column(nullabletrue)]
  43.     private ?bool $featured null;
  44.     #[ORM\Column(typeTypes::SMALLINTnullabletrue)]
  45.     private ?int $homepageOrder null;
  46.     #[ORM\OneToMany(targetEntitySubcategory::class, mappedBy'category'cascade: ['persist''remove'])]
  47.     private Collection $subcategories;
  48.     public function __construct()
  49.     {
  50.         $this->products = new ArrayCollection();
  51.         $this->users = new ArrayCollection();
  52.         $this->subcategories = new ArrayCollection();
  53.     }
  54.     public function __call($method$arguments)
  55.     {
  56.         return $this->proxyCurrentLocaleTranslation($method$arguments);
  57.     }
  58.     public function __toString()
  59.     {
  60.         return $this->translate($this->getCurrentLocale())->getName();
  61.     }
  62.     #[ORM\PrePersist]
  63.     public function setSlugValue(){
  64.         $this->setSlug($this->translate('es')->getName());
  65.     }
  66.     public function getId(): ?int
  67.     {
  68.         return $this->id;
  69.     }
  70.     public function getSlug(): ?string
  71.     {
  72.         return $this->slug;
  73.     }
  74.     public function setSlug(string $slug): self
  75.     {
  76.         $this->slug $slug;
  77.         return $this;
  78.     }
  79.     
  80.     /**
  81.      * @return Collection<int, Product>
  82.      */
  83.     public function getProducts(): Collection
  84.     {
  85.         return $this->products;
  86.     }
  87.     public function addProduct(Product $product): self
  88.     {
  89.         if (!$this->products->contains($product)) {
  90.             $this->products->add($product);
  91.             $product->addCategory($this);
  92.         }
  93.         return $this;
  94.     }
  95.     public function removeProduct(Product $product): self
  96.     {
  97.         if ($this->products->removeElement($product)) {
  98.             $product->removeCategory($this);
  99.         }
  100.         return $this;
  101.     }
  102.     /**
  103.      * @return Collection<int, User>
  104.      */
  105.     public function getUsers(): Collection
  106.     {
  107.         return $this->users;
  108.     }
  109.     public function addUser(User $user): self
  110.     {
  111.         if (!$this->users->contains($user)) {
  112.             $this->users->add($user);
  113.             $user->addCategoryInterest($this);
  114.         }
  115.         return $this;
  116.     }
  117.     public function removeUser(User $user): self
  118.     {
  119.         if ($this->users->removeElement($user)) {
  120.             $user->removeCategoryInterest($this);
  121.         }
  122.         return $this;
  123.     }
  124.     public function getIcon(): ?Media
  125.     {
  126.         return $this->icon;
  127.     }
  128.     public function setIcon(?Media $icon): self
  129.     {
  130.         $icon->setDirectory('category');
  131.         $this->icon $icon;
  132.         return $this;
  133.     }
  134.     public function isActive(): ?bool
  135.     {
  136.         return $this->active;
  137.     }
  138.     public function setActive(?bool $active): self
  139.     {
  140.         $this->active $active;
  141.         return $this;
  142.     }
  143.     public function isFeatured(): ?bool
  144.     {
  145.         return $this->featured;
  146.     }
  147.     public function setFeatured(?bool $featured): self
  148.     {
  149.         $this->featured $featured;
  150.         return $this;
  151.     }
  152.     public function getHomepageOrder(): ?int
  153.     {
  154.         return $this->homepageOrder;
  155.     }
  156.     public function setHomepageOrder(?int $homepageOrder): self
  157.     {
  158.         $this->homepageOrder $homepageOrder;
  159.         return $this;
  160.     }
  161.     /**
  162.      * @return Collection<int, Category>
  163.      */
  164.     public function getSubcategories(): Collection
  165.     {
  166.         return $this->subcategories;
  167.     }
  168.     public function addSubcategory(Subcategory $subcategory): self
  169.     {
  170.         if (!$this->subcategories->contains($subcategory)) {
  171.             $this->subcategories[] = $subcategory;
  172.             $subcategory->setCategory($this);
  173.         }
  174.         return $this;
  175.     }
  176.     public function removeSubcategory(Subcategory $subcategory): self
  177.     {
  178.         if ($this->subcategories->removeElement($subcategory)) {
  179.             // set the owning side to null (unless already changed)
  180.             if ($subcategory->getCategory() === $this) {
  181.                 $subcategory->setCategory(null);
  182.             }
  183.         }
  184.         return $this;
  185.     }
  186. }