<?php
namespace App\Entity;
use App\Repository\CategoriesRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\MaxDepth;
/**
* @Gedmo\Tree(type="nested")
* @ORM\Entity(repositoryClass=CategoriesRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class Categories
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer", options={"unsigned":true})
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity=Attributes::class, inversedBy="categories")
* @MaxDepth(1)
*/
private $attribute;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $options;
/**
* @Gedmo\TreeRoot
* @ORM\ManyToOne(targetEntity="Categories")
* @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
* @MaxDepth(1)
*/
private $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="Categories", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
* @MaxDepth(1)
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Categories", mappedBy="parent")
* @ORM\OrderBy({"sysLft" = "ASC"})
* @MaxDepth(1)
*/
private $children;
/**
* @Gedmo\TreeLeft
* @ORM\Column(type="integer", nullable=true)
*/
private $sysLft;
/**
* @Gedmo\TreeRight
* @ORM\Column(type="integer", nullable=true)
*/
private $sysRgt;
/**
* @Gedmo\TreeLevel
* @ORM\Column(type="integer", nullable=true)
*/
private $sysLevel;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $active;
/**
* @ORM\Column(type="integer")
*/
private $createdAt;
/**
* @ORM\Column(type="integer")
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity=Catalog::class, mappedBy="category")
*/
private $catalogs;
/**
* @ORM\OneToMany(targetEntity=Promo::class, mappedBy="category")
* @MaxDepth(1)
*/
private $promos;
public function __construct()
{
$this->catalogs = new ArrayCollection();
$this->promos = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getAttribute(): ?Attributes
{
return $this->attribute;
}
public function setAttribute(?Attributes $attribute): self
{
$this->attribute = $attribute;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getOptions(): ?string
{
return $this->options;
}
public function setOptions(?string $options): self
{
$this->options = $options;
return $this;
}
public function getRoot(): ?int
{
return $this->root;
}
public function setRoot(?int $root): self
{
$this->root = $root;
return $this;
}
public function setParent($parent = null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
public function getSysLft(): ?int
{
return $this->sysLft;
}
public function setSysLft(?int $sysLft): self
{
$this->sysLft = $sysLft;
return $this;
}
public function getSysRgt(): ?int
{
return $this->sysRgt;
}
public function setSysRgt(?int $sysRgt): self
{
$this->sysRgt = $sysRgt;
return $this;
}
public function getSysLevel(): ?int
{
return $this->sysLevel;
}
public function setSysLevel(?int $sysLevel): self
{
$this->sysLevel = $sysLevel;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps(): void
{
$dateTimeNow = (new DateTime())->getTimestamp();
$this->setUpdatedAt($dateTimeNow);
if ($this->getCreatedAt() === null) {
$this->setCreatedAt($dateTimeNow);
}
}
public function getCreatedAt(): ?int
{
return $this->createdAt;
}
public function setCreatedAt(int $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?int
{
return $this->updatedAt;
}
public function setUpdatedAt(int $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection|Catalog[]
*/
public function getCatalogs(): Collection
{
return $this->catalogs;
}
public function addCatalog(Catalog $catalog): self
{
if (!$this->catalogs->contains($catalog)) {
$this->catalogs[] = $catalog;
$catalog->setCategory($this);
}
return $this;
}
public function removeCatalog(Catalog $catalog): self
{
if ($this->catalogs->contains($catalog)) {
$this->catalogs->removeElement($catalog);
// set the owning side to null (unless already changed)
if ($catalog->getCategory() === $this) {
$catalog->setCategory(null);
}
}
return $this;
}
/**
* @return Collection|Promo[]
*/
public function getPromos(): Collection
{
return $this->promos;
}
public function addPromo(Promo $promo): self
{
if (!$this->promos->contains($promo)) {
$this->promos[] = $promo;
$promo->setCategory($this);
}
return $this;
}
public function removePromo(Promo $promo): self
{
if ($this->promos->contains($promo)) {
$this->promos->removeElement($promo);
// set the owning side to null (unless already changed)
if ($promo->getCategory() === $this) {
$promo->setCategory(null);
}
}
return $this;
}
}
<?php
namespace App\Entity;
use App\Repository\CatalogRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\MaxDepth;
/**
* @ORM\Entity(repositoryClass=CatalogRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class Catalog
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer", options={"unsigned":true})
*
* @Groups({"json-search"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $sku;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @Groups({"json-search"})
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=Brands::class, inversedBy="catalogs")
* @MaxDepth(1)
*/
private $brand;
/**
* @ORM\ManyToOne(targetEntity=Categories::class, inversedBy="catalogs")
* @MaxDepth(1)
*/
private $category;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $shortInfo;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="string", length=1000, nullable=true)
*
* @Groups({"json-search"})
*/
private $images;
/**
* @ORM\Column(type="boolean")
*/
private $active;
/**
* @ORM\Column(type="integer")
*/
private $createdAt;
/**
* @ORM\Column(type="integer")
*/
private $updatedAt;
/**
* @ORM\ManyToMany(targetEntity=Promo::class, mappedBy="product")
* @MaxDepth(1)
*/
private $promos;
/**
* @ORM\OneToOne(targetEntity=CatalogPrice::class, mappedBy="product", cascade={"persist", "remove"})
* @MaxDepth(1)
*/
private $catalogPrice;
/**
* @ORM\OneToMany(targetEntity=Cart::class, mappedBy="product")
* @MaxDepth(1)
*/
private $carts;
/**
* @ORM\OneToMany(targetEntity=OrdersProducts::class, mappedBy="product")
* @Exclude()
* @MaxDepth(1)
*/
private $ordersProducts;
/**
* @ORM\Column(type="string", length=255, nullable=true, unique=true)
*/
private $uid;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $options;
public function __construct()
{
$this->promos = new ArrayCollection();
$this->carts = new ArrayCollection();
$this->ordersProducts = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getSku(): ?string
{
return $this->sku;
}
public function setSku(?string $sku): self
{
$this->sku = $sku;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getBrand(): ?Brands
{
return $this->brand;
}
public function setBrand(?Brands $brand): self
{
$this->brand = $brand;
return $this;
}
public function getCategory(): ?Categories
{
return $this->category;
}
public function setCategory(?Categories $category): self
{
$this->category = $category;
return $this;
}
public function getShortInfo(): ?string
{
return $this->shortInfo;
}
public function setShortInfo(?string $shortInfo): self
{
$this->shortInfo = $shortInfo;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getImages(): ?string
{
return $this->images;
}
public function setImages(?string $images): self
{
$this->images = $images;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps(): void
{
$dateTimeNow = (new DateTime())->getTimestamp();
$this->setUpdatedAt($dateTimeNow);
if ($this->getCreatedAt() === null) {
$this->setCreatedAt($dateTimeNow);
}
}
public function getCreatedAt(): ?int
{
return $this->createdAt;
}
public function setCreatedAt(int $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?int
{
return $this->updatedAt;
}
public function setUpdatedAt(int $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @return Collection|Promo[]
*/
public function getPromos(): Collection
{
return $this->promos;
}
public function addPromo(Promo $promo): self
{
if (!$this->promos->contains($promo)) {
$this->promos[] = $promo;
$promo->addProduct($this);
}
return $this;
}
public function removePromo(Promo $promo): self
{
if ($this->promos->contains($promo)) {
$this->promos->removeElement($promo);
$promo->removeProduct($this);
}
return $this;
}
public function getCatalogPrice(): ?CatalogPrice
{
return $this->catalogPrice;
}
public function setCatalogPrice(?CatalogPrice $catalogPrice): self
{
$this->catalogPrice = $catalogPrice;
// set (or unset) the owning side of the relation if necessary
$newProduct = null === $catalogPrice ? null : $this;
if ($catalogPrice->getProduct() !== $newProduct) {
$catalogPrice->setProduct($newProduct);
}
return $this;
}
/**
* @return Collection|Cart[]
*/
public function getCarts(): Collection
{
return $this->carts;
}
public function addCart(Cart $cart): self
{
if (!$this->carts->contains($cart)) {
$this->carts[] = $cart;
$cart->setProduct($this);
}
return $this;
}
public function removeCart(Cart $cart): self
{
if ($this->carts->contains($cart)) {
$this->carts->removeElement($cart);
// set the owning side to null (unless already changed)
if ($cart->getProduct() === $this) {
$cart->setProduct(null);
}
}
return $this;
}
/**
* @return Collection|OrdersProducts[]
*/
public function getOrdersProducts(): Collection
{
return $this->ordersProducts;
}
public function addOrdersProduct(OrdersProducts $ordersProduct): self
{
if (!$this->ordersProducts->contains($ordersProduct)) {
$this->ordersProducts[] = $ordersProduct;
$ordersProduct->setProduct($this);
}
return $this;
}
public function removeOrdersProduct(OrdersProducts $ordersProduct): self
{
if ($this->ordersProducts->contains($ordersProduct)) {
$this->ordersProducts->removeElement($ordersProduct);
// set the owning side to null (unless already changed)
if ($ordersProduct->getProduct() === $this) {
$ordersProduct->setProduct(null);
}
}
return $this;
}
public function getUid(): ?string
{
return $this->uid;
}
public function setUid(?string $uid): self
{
$this->uid = $uid;
return $this;
}
public function getOptions(): ?string
{
return $this->options;
}
public function setOptions(?string $options): self
{
$this->options = $options;
return $this;
}
}
Этот метод из репозитория, который я привожу выше. Я здесь закомментировал строку с джоином, так как от него не было результата. Т.е. queryBuilder и так подтягивает связи, раз у меня и без join все отображается на странице.
Вы сказали про ленивую загрузку. Когда я делал выборку с помощью метода findBy(), этой проблемы не было, т.е. не было избыточных запросов. Также я включал fetch"EAGER" у Catalog к CatalogPrice, без толку.
Запрос к категории делаю чтобы получить текущую категорию, она к общему списку дела не имеет.
В сущностях у меня все джоины на месте. Про ленивый геттер не совсем понял что вы имеете ввиду.