src/Entity/Subcategory.php line 24

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