<?php
namespace App\Entity;
use App\Traits\PictureTrait;
use App\Traits\ProductTrait;
use App\Traits\SortTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Vat;
use App\Entity\Picture;
use Knp\DoctrineBehaviors\Contract\Entity\SoftDeletableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletableTrait;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Component\VarDumper\VarDumper;
/**
* Product
*
* @ORM\Table(name="catalog_product")
* @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
* @ORM\AssociationOverrides({
* @ORM\AssociationOverride(name="pictures",
* joinTable=@ORM\JoinTable(
* name="catalog_product_pictures"
* ),
* joinColumns=@ORM\JoinColumn(
* name="product_id", referencedColumnName="id"
* )
* )
* })
*/
class Product implements TimestampableInterface, TranslatableInterface, SoftDeletableInterface
{
use TimestampableTrait,
TranslatableTrait,
SoftDeletableTrait,
SortTrait,
ProductTrait,
PictureTrait;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="products", cascade={"persist"})
**/
private $category;
/**
* @ORM\ManyToMany(targetEntity="Product")
* @ORM\JoinTable(name="catalog_product_related_product",
* joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="related_product_id", referencedColumnName="id")}
* )
*/
private $relatedProducts;
/**
* @var boolean
*
* @ORM\Column(type="boolean", name="display_on_shop", nullable=true)
*/
private $displayOnShop;
public function __construct()
{
$this->relatedProducts = new ArrayCollection();
}
public function __toString()
{
$result = "";
$i = 1;
foreach ($this->getTranslations() as $translation) {
$result .= $translation->getTitle();
if ($i != count($this->getTranslations())) {
$result .= " - ";
}
$i++;
}
return $result;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
public function addRelatedProduct(Product $p)
{
$this->relatedProducts[] = $p;
}
public function removeRelatedProduct(Product $p)
{
$this->relatedProducts->removeElement($p);
}
/**
* @return mixed
*/
public function getRelatedProducts()
{
return $this->relatedProducts;
}
public function getRelatedProductsOnline()
{
$criteria = Criteria::create()
->andWhere(Criteria::expr()->eq('deletedAt', null));
return $this->relatedProducts->matching($criteria);
}
/**
* Get Category
*
* @return Category
*
*/
public function getCategory()
{
return $this->category;
}
/**
* Set Category
*
* @param Category $category
* @return Product
*/
public function setCategory(Category $category)
{
$this->category = $category;
return $this;
}
public function isDisplayOnShop(): bool
{
return $this->displayOnShop;
}
public function setDisplayOnShop(bool $displayOnShop): void
{
$this->displayOnShop = $displayOnShop;
}
}