1

Gedmo Tree could be used for example with Projects and sub-products and so on. But i've an situation where i have a connection table between Products and Categories (ManytoMany).

Each product could be connected through many Categories. And each Category could have many Products. In my product view there is an option to connect multiple categories.

The problem i have is a bit difficult to explain. I've a view with all categories where a category could have sub categories.

  1. cat1 (show products)

    1.1 cat1-1 (show products)

    1.2 cat1-2 (show products)

Each categorie, even sub categories haves a link, that opens a new page with all theire connected products. I need to find a way to sort these products based on theire category, what i'm trying to do with Gedmo Tree.

See entity below which is the connection entitiy.

The problem is that Gedmo doesn't distinct the category where you in. It just continues with counting the lft and rgt.

I hope i've discribe my problem cleary and that somebody have an sollution.

Maybe i could use Gedmo treeRoot? But don't know where treeRoot could be used for

<?php
/**
 * ProductCategory
 */
namespace XBS\AppBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * ProductCategory
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ProductCategoryRepository")
 * @Gedmo\Tree(type="nested")
 * ...
 */
class ProductCategory
{

    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Product", inversedBy="productCategories")
     * @Assert\NotNull()
     */
    protected $product;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="productCategories")
     * @Assert\NotNull()
     */
    protected $category;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="children" )
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $parent;

    /**
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Category", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    protected $children;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * setProduct
     * 
     * @param null $product
     * @return $this
     */
    public function setProduct($product = null)
    {
        $this->product = $product;

        return $this;
    }


    /**
     * getProduct
     * @return mixed
     */
    public function getProduct()
    {
        return $this->product;
    }

    /**
     * setCategory
     * 
     * @param null $category
     * @return $this
     */
    public function setCategory($category = null)
    {
        $this->category = $category;

        return $this;
    }

    /**
     * getCategory
     * @return mixed
     */
    public function getCategory()
    {
        return $this->category;
    }

    /**
     *  @return String empty
     */
    public function __toString(){
        return '';
    }
}
Bham
  • 309
  • 5
  • 21
  • Hy, the nested tree must be on a Category entity, and between the Product and Category entities you can have a ProductCategory entity where the position of the product in the affected category is store. I hope it's clear – Pec Jul 20 '18 at 15:19
  • @Pec but how could i change the position from a product. For Example cat 1-1 has 10 products and i want to change the position of product 3 to position 1 . Does Every row needs to be updated with the new order or something like that – Bham Jul 20 '18 at 15:40
  • Yep, if you want a good positioning you must update each products for the selected category – Pec Jul 20 '18 at 15:52
  • Is updating possible or does i need to remove everything and then insert the new rows with order or is there a way to contain the rows and just update the position in Some kind a way – Bham Jul 20 '18 at 16:25
  • You can just update, it's up to you to see how to implement it – Pec Jul 23 '18 at 14:37
  • I’ve fixed it with https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sortable.md. Where you could use group functionality for grouping the postition. In this case the grouping is based on categorie. Also used https://medium.com/@treetop1500/setting-up-a-sortable-drag-n-drop-interface-for-symfony-entities-7f0c84ac0c8e for drag and drop as base – Bham Jul 23 '18 at 15:45

0 Answers0