S
S
svscorp2013-06-08 12:37:44
symfony
svscorp, 2013-06-08 12:37:44

Bundle to categorize any nesting for Symfony2?

Hey!
Colleagues, has anyone come across a Bundle for categorizing any nesting for Symfony2 or just a good ready-made solution in PHP, c?
Those. let's say I want to have 4 root categories, each of them can have sub-categories, which, in turn, have more sub-categories.
Shoes
- Sportswear
- - Brand1
- - Brand2
- Casual
Wear
- Jackets
...
routing /categories/shoes/brand1 etc.
Relatively small problem, but I don't want to reinvent the wheel. Maybe someone met something similar on the net.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
svscorp, 2013-06-14
@svscorp

By the way, the actual implementation of the solution ... suddenly come in handy:
1) Create an Entity:

<?php
// src/Acme/DemoBundle/Entity/Category.php
namespace Acme\DemoBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="categories")
 * use repository for handy tree functions
 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
 */
class Category
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

    /**
     * @Gedmo\Translatable
     * @ORM\Column(name="title", type="string", length=64)
     */
    private $title;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(name="lft", type="integer")
     */
    private $lft;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(name="lvl", type="integer")
     */
    private $lvl;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(name="rgt", type="integer")
     */
    private $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\Column(name="root", type="integer", nullable=true)
     */
    private $root;

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

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

    /**
     *

     * @Gedmo\Translatable

     * @Gedmo\Slug(fields={"title"})

     * @ORM\Column(name="slug", type="string", length=128)

     */

    private $slug;

    public function getId()
    {
        return $this->id;
    }

    public function getSlug()

    {

        return $this->slug;

    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setParent(Category $parent = null)
    {
        $this->parent = $parent;
    }

    public function getParent()
    {
        return $this->parent;
    }
}

2. In the controller, we implement the addition of categories:
            use Acme\DemoBundle\Entity\Category as CategoryEntity;

            // ... your code before
            $em = $this->getDoctrine()->getManager();
            $cat1= new CategoryEntity();
            $cat1->setTitle('Фрукты');
            
            $subcat = new CategoryEntity();
            $subcat->setTitle('Экзотические');
            $subcat->setParent($cat1);
     
            $cat2 = new CategoryEntity();
            $cat2->setTitle('Овощи');

            $em->persist($cat1);
            $em->persist($cat2);
            $em->persist($subcat);
            $em->flush();
     

You will get a menu of the form:
- Fruits
- - Exotic
- Vegetables
3) Get a list of categories (in the form of an array):
// ...
$categoryEntity = $this->em->getRepository('Acme\DemoBundle\Entity\Category');
$categories = $categoryEntity->childrenHierarchy();
// ...

Good luck!

N
Nikita Gusakov, 2013-06-08
@hell0w0rd

I came across this problem just recently. I don’t know about bundles - there is an extension for doctrine github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/tree.md

S
skochwm, 2014-06-26
@skochwm

I tried to implement according to your example, I copied everything 1 to 1. But I got an error:
An exception occurred while executing 'INSERT INTO prod_category (title, lft, lvl, rgt, root, slug, parent_id) VALUES (?, ?, ?, ?, ?, ?, ?)' with params ["\u0424\u0440\u0443\u043a\u0442\u044b", null, null, null, null, null, null]:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column ' lft' cannot be null
500 Internal Server Error - DBALException
1 linked Exception:
• PDOException »

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question