src/Entity/Product.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Service\FileHandlerService;
  4. use App\Repository\ProductRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Serializer\Annotation as Serializer;
  9. use Symfony\Component\Serializer\Annotation\SerializedName;
  10. use App\Annotation\DashboardAnnotation as Dashboard;
  11. use App\Annotation\FormBuildAnnotation as FormBuild;
  12. /**
  13.  * @ORM\Entity(repositoryClass=ProductRepository::class)
  14.  * @Dashboard(generation=true, name="Продукция")
  15.  * @ORM\Table(indexes={
  16.  *     @ORM\Index(name="product_title_idx", columns={"title"})
  17.  * })
  18.  */
  19. class Product
  20. {
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      * @Serializer\Groups({"collection", "selection/data", "selection/id"})
  26.      * @Dashboard(label="Номер", only=true)
  27.      */
  28.     private $id;
  29.     /**
  30.      * @ORM\Column(type="string", length=255)
  31.      * @Serializer\Groups({"collection", "selection/data", "selection/id"}) 
  32.      * @Dashboard(label="Название продукции")
  33.      * @FormBuild(component="input")
  34.      */
  35.     private $title;
  36.     /**
  37.      * @ORM\ManyToOne(targetEntity=Type::class, inversedBy="products")
  38.      * @ORM\JoinColumn(nullable=true)
  39.      * @Serializer\Groups({"collection", "selection/data", "selection/id"})
  40.      * @Dashboard(label="Тип")
  41.      * @FormBuild(component="autocomplete", bindField="type", field="title",  params={"nullable": true})
  42.      */
  43.     private $type;
  44.     /**
  45.      * @ORM\ManyToOne(targetEntity=Season::class, inversedBy="products")
  46.      * @ORM\JoinColumn(nullable=true)
  47.      * @Serializer\Groups({"collection", "selection/data", "selection/id"})
  48.      * @Dashboard(label="Сезон")
  49.      * @FormBuild(component="autocomplete", bindField="season", field="title",  params={"nullable": true})
  50.      */
  51.     private $season;
  52.     /**
  53.      * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="products")
  54.      * @ORM\JoinColumn(nullable=true)
  55.      * @Serializer\Groups({"collection", "selection/id"})
  56.      * @Dashboard(label="Категория")
  57.      * @FormBuild(component="autocomplete", bindField="category", field="title",  params={"nullable": true})
  58.      */
  59.     private $category;
  60.     /**
  61.      * @ORM\Column(type="string", length=255, nullable=true)
  62.      * @FormBuild(component="files", multiple=false, accept="image/*")
  63.      * @Dashboard(label="Превью", only=true, head={"type":"image", "value":"file.path"})
  64.      */
  65.     public $file;
  66.     /**
  67.      * @Serializer\Groups({"collection", "selection/data", "selection/id"})
  68.      */
  69.     public function getFile() {
  70.         $fileHandler = new FileHandlerService();
  71.         
  72.         if (!$this->file)
  73.             return null;
  74.         return [
  75.             'name' => $this->file,
  76.             'path' => '/'.$fileHandler->getGlobalPath().'/product/'.$this->file
  77.         ];
  78.     }
  79.     /**
  80.      * @ORM\Column(type="boolean")
  81.      * @Serializer\Groups({"collection"}) 
  82.      * @FormBuild(component="switch")
  83.      * @Dashboard(label="Скрыть на сайте", only=true, inverse=true)
  84.      */
  85.     private $public true;
  86.     /**
  87.      * @Serializer\Groups({"collection", "selection/data"})
  88.      * @SerializedName("articles") 
  89.      * @ORM\OneToMany(targetEntity=ProductArticle::class, mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  90.      * @ORM\OrderBy({"title" = "ASC"})
  91.      */
  92.     private $productArticles;
  93.     /**
  94.      * @ORM\OneToMany(targetEntity=ProductOem::class, mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  95.      */
  96.     private $productOems;
  97.     public function __construct()
  98.     {
  99.         $this->productArticles = new ArrayCollection();
  100.         $this->selections = new ArrayCollection();
  101.         $this->productOems = new ArrayCollection();
  102.     }
  103.     public function getId(): ?int
  104.     {
  105.         return $this->id;
  106.     }
  107.     public function getTitle(): ?string
  108.     {
  109.         return $this->title;
  110.     }
  111.     public function setTitle(string $title): self
  112.     {
  113.         $this->title $title;
  114.         return $this;
  115.     }
  116.     public function isPublic(): ?bool
  117.     {
  118.         return $this->public;
  119.     }
  120.     public function setPublic(bool $public): self
  121.     {
  122.         $this->public $public;
  123.         return $this;
  124.     }
  125.     public function getType(): ?Type
  126.     {
  127.         return $this->type;
  128.     }
  129.     public function setType(?Type $type): self
  130.     {
  131.         $this->type $type;
  132.         return $this;
  133.     }
  134.     public function getSeason(): ?Season
  135.     {
  136.         return $this->season;
  137.     }
  138.     public function setSeason(?Season $season): self
  139.     {
  140.         $this->season $season;
  141.         return $this;
  142.     }
  143.     public function getFileName(): ?string
  144.     {
  145.         return $this->file;
  146.     }
  147.     public function setFile(?string $file): self
  148.     {
  149.         $this->file $file;
  150.         return $this;
  151.     }
  152.     public function getCategory(): ?Category
  153.     {
  154.         return $this->category;
  155.     }
  156.     public function setCategory(?Category $category): self
  157.     {
  158.         $this->category $category;
  159.         return $this;
  160.     }
  161.     /**
  162.      * @return Collection<int, ProductArticle>
  163.      */
  164.     public function getProductArticles(): Collection
  165.     {
  166.         return $this->productArticles;
  167.     }
  168.     public function addProductArticle(ProductArticle $productArticle): self
  169.     {
  170.         if (!$this->productArticles->contains($productArticle)) {
  171.             $this->productArticles[] = $productArticle;
  172.             $productArticle->setProduct($this);
  173.         }
  174.         return $this;
  175.     }
  176.     public function removeProductArticle(ProductArticle $productArticle): self
  177.     {
  178.         if ($this->productArticles->removeElement($productArticle)) {
  179.             // set the owning side to null (unless already changed)
  180.             if ($productArticle->getProduct() === $this) {
  181.                 $productArticle->setProduct(null);
  182.             }
  183.         }
  184.         return $this;
  185.     }
  186.     public function clearArticles():self  
  187.     {
  188.         $this->productArticles = new ArrayCollection();
  189.         return $this;
  190.     }
  191.     /**
  192.      * @return Collection<int, ProductOem>
  193.      */
  194.     public function getProductOems(): Collection
  195.     {
  196.         return $this->productOems;
  197.     }
  198.     public function addProductOem(ProductOem $productOem): self
  199.     {
  200.         if (!$this->productOems->contains($productOem)) {
  201.             $this->productOems[] = $productOem;
  202.             $productOem->setProduct($this);
  203.         }
  204.         return $this;
  205.     }
  206.     public function getProductOemsAsArray(): array
  207.     {
  208.         $result = [];
  209.         foreach($this->getProductOems() as $oem) {
  210.             $result[] = $oem->getOem();
  211.         }
  212.         return $result;
  213.     }
  214.     public function removeProductOem(ProductOem $productOem): self
  215.     {
  216.         if ($this->productOems->removeElement($productOem)) {
  217.             // set the owning side to null (unless already changed)
  218.             if ($productOem->getProduct() === $this) {
  219.                 $productOem->setProduct(null);
  220.             }
  221.         }
  222.         return $this;
  223.     }
  224.     public function clearOems(): self  
  225.     {
  226.         $this->productOems = new ArrayCollection();
  227.         return $this;
  228.     }
  229.     public function clearArticlesAndOems() 
  230.     {
  231.         return $this->clearOems()->clearArticles();
  232.     }
  233.     /**
  234.      * @Serializer\Groups({"selection/id"}) 
  235.      * @SerializedName("article") 
  236.      */
  237.     public function getOneProductArticle(): ?ProductArticle
  238.     {
  239.         foreach($this->getProductArticles() as $article) {
  240.             return $article;
  241.         }
  242.         return null;
  243.     }
  244. }