PHP code example of bupy7 / doctrine-nested-set

1. Go to this page and download the library: Download bupy7/doctrine-nested-set library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bupy7 / doctrine-nested-set example snippets


use Bupy7\Doctrine\NestedSet\NestedSetInterface;

/**
 * @Entity(repositoryClass="CategoryRepository")
 */
class Category implements NestedSetInterface
{
    /**
     * @Id
     * @Column(type="integer")
     * @GeneratedValue
     * @var int|null
     */
    private $id;
    /**
     * @Column(type="integer", name="root_key")
     * @var int
     */
    private $rootKey = 1;
    /**
     * @Column(type="integer")
     * @var int
     */
    private $level = 1;
    /**
     * @Column(type="integer", name="left_key")
     * @var int
     */
    private $leftKey;
    /**
     * @Column(type="integer", name="right_key")
     * @var int
     */
    private $rightKey;
    /**
     * @Column(type="string")
     * @var string
     */
    private $name;

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

    public function setId($id): NestedSetInterface
    {
        $this->id = $id;
        return $this;
    }
    
    public function getRootKey(): int
    {
        return $this->rootKey;
    }

    public function setRootKey(int $rootKey): NestedSetInterface
    {
        $this->rootKey = $rootKey;
        return $this;
    }

    public function getLevel(): int
    {
        return $this->level;
    }

    public function setLevel(int $level): NestedSetInterface
    {
        $this->level = $level;
        return $this;
    }

    public function getLeftKey(): int
    {
        return $this->leftKey;
    }

    public function setLeftKey(int $leftKey): NestedSetInterface
    {
        $this->leftKey = $leftKey;
        return $this;
    }

    public function getRightKey(): int
    {
        return $this->rightKey;
    }

    public function setRightKey(int $rightKey): NestedSetInterface
    {
        $this->rightKey = $rightKey;
        return $this;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): Category
    {
        $this->name = $name;
        return $this;
    }
}

use Bupy7\Doctrine\NestedSet\NestedSetRepositoryAbstract;

class CategoryRepository extends NestedSetRepositoryAbstract
{
}

$categories = $categoryRepository->findAll();

// var_dump($categories);
//
// - PC
// - Motherboards
// - RAM
// - DDR3
// - DDR4
// - CPU
// - Laptops
// - Tablets

$parentCategory = $categoryRepository->findOneByName('RAM');
$children = $categoryRepository->findChildren($parentCategory);

// var_dump($children);
//
// - DDR3
// - DDR4

$parentCategory = $categoryRepository->findOneByName('PC');
$descendants = $categoryRepository->findDescendants($parentCategory);

// var_dump($children);
//
// - Motherboards
// - RAM
// - DDR3
// - DDR4
// - CPU

$childrenCategory = $categoryRepository->findOneByName('RAM');
$parent = $categoryRepository->findOneParent($childrenCategory);

// var_dump($parent);
//
// - PC

$roots = $categoryRepository->findRoots();

// var_dump($parent);
//
// - PC
// - Laptops
// - Tablets

$childrenCategory = $categoryRepository->findOneByName('DDR3');
$roots = $categoryRepository->findAncestors($childrenCategory);

// var_dump($parent);
//
// - RAM
// - PC

$category = new Category();
$category->setName('DDR2');

$parentCategory = $categoryRepository->findOneByName('RAM');
$categoryRepository->prepend($category, $parentCategory);
$entityManager->clear(); // optional, if you need

$category = new Category();
$category->setName('LGA 1151v2');

$parentCategory = $categoryRepository->findOneByName('CPU');
$categoryRepository->append($category, $parentCategory);
$entityManager->clear(); // optional, if you need

$category = new Category();
$category->setName('Phones');

$categoryRepository->prepend($category);
$entityManager->clear(); // optional, if you need

$category = new Category();
$category->setName('Phones');

$categoryRepository->append($category);
$entityManager->clear(); // optional, if you need

$category = $categoryRepository->findOneByName('CPU');
$categoryRepository->remove($category);
$entityManager->clear(); // optional, if you need

$category = $categoryRepository->findOneByName('PC');
$categoryRepository->remove($category);
$entityManager->clear(); // optional, if you need

use Doctrine\DBAL\TransactionIsolationLevel;

// if you want to change isolation level
// $oldIsolationLevel = $entityManager->getConnection()->getTransactionIsolation();
// $entityManager->getConnection()->setTransactionIsolation(TransactionIsolationLevel::SERIALIZABLE);

$entityManager->beginTransaction();
try {
    $category = $categoryRepository->findOneByName('PC');
    $categoryRepository->remove($category);
    $entityManager->commit();
    $entityManager->clear(); // optional, if you need
} catch (Exception $e) {
    $entityManager->rollback();
    throw $e;
} finally {
    // if you changed isolation level
    // $entityManager->getConnection()->setTransactionIsolation($oldIsolationLevel);
}