Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arodax/doctrine-extensions-tree
Doctrine Extension providing Tree hierarchy
https://github.com/arodax/doctrine-extensions-tree
doctrine orm symfony tree
Last synced: 2 months ago
JSON representation
Doctrine Extension providing Tree hierarchy
- Host: GitHub
- URL: https://github.com/arodax/doctrine-extensions-tree
- Owner: arodax
- License: mit
- Created: 2019-01-24T06:04:26.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-10-13T22:41:54.000Z (about 2 years ago)
- Last Synced: 2024-10-02T22:54:32.018Z (3 months ago)
- Topics: doctrine, orm, symfony, tree
- Language: PHP
- Homepage:
- Size: 206 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hierarchical Tree extension for Doctrine
![Licence MIT](https://img.shields.io/packagist/l/arodax/doctrine-extensions-tree?style=flat)
![Build Status](https://api.travis-ci.org/arodax/doctrine-extensions-tree.svg?branch=master&style=flat "Build Status")
![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat&color=orange)
![downloads total](https://img.shields.io/packagist/dt/doctrine-extensions/tree?color=blue&style=flat)This extension allows you to store your data in hierarchicaly in your database using Doctrine ORM.
Note: this code is the hard fork of
[Atlantic18/DoctrineExtensions](https://github.com/Atlantic18/DoctrineExtensions) 2.4.x branch of the Tree extension only.## Licence
MIT
## Changelog
- 3.2.3 Fixed cache item id generator
- 3.2.2 Automatically cache entity metadata when symfony/cache is installed.
- 3.2.1 Fixed class and file name mismatch for XML driver
- 3.2.0 Deprecated TreeRight, TreeLeft, TreeClosure, TreeRoot, TreePath, TreeLevel annotations, use them without "Tree" prefix (e.g. Right, Left, Closure ...)
- 3.2.0 Added native PHP attributes support
- 3.1.0 Renamed **TreeAdapterInterface** to **AdapterInterface**
- 3.0.0 Changed namespace to `Arodax\Doctrine\Extensions\Tree`, package has been renamed to `arodax/doctrine-extensions-tree`.
Make sure you **change path and the namespace in config/packages/doctrine.yaml** - see installation guide bellow for the example!
- 2.0.0 Minimum compatible version of doctrine/common package has been increased to 3.0.*
- 1.0.3 Implementation of [#2020](https://github.com/Atlantic18/DoctrineExtensions/pull/2020) removed instances of hard coded parent column in queries
- 1.0.2 Added missing repositories from the original extension- 1.0.1 Implementation of [#2001](https://github.com/Atlantic18/DoctrineExtensions/pull/2001) fixing problem causing wrong left/right order.
## Installation
Install the extension with the [composer](https://getcomposer.org)`composer require arodax/doctrine-extensions-tree`
### Using in the Symfony project
There is no flex recipe yet, so you need to manually enable extension by adding the following into your configuration files**config/packages/doctrine.yaml**
```yaml
parameters:
...
doctrine:
dbal:
...
orm:
...
mappings:
...
Arodax\Doctrine\Extensions\Tree:
is_bundle: false
type: annotation #attribute
dir: '%kernel.project_dir%/vendor/arodax/doctrine-extensions-tree/src/Entity'
prefix: 'Arodax\Doctrine\Extensions\Tree\Entity'
```**config/services/doctrine.yaml**
```yaml
parameters:
...services:
...
Arodax\Doctrine\Extensions\Tree\TreeSubscriber:
class: Arodax\Doctrine\Extensions\Tree\TreeSubscriber
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ '@annotation_reader' ] ]
```## Prepare entity for the hierarchical tree
### Annotate your entity:
```php
setTitle('Food');$fruits = new Category();
$fruits->setTitle('Fruits');
$fruits->setParent($food);$vegetables = new Category();
$vegetables->setTitle('Vegetables');
$vegetables->setParent($food);$carrots = new Category();
$carrots->setTitle('Carrots');
$carrots->setParent($vegetables);$this->em->persist($food);
$this->em->persist($fruits);
$this->em->persist($vegetables);
$this->em->persist($carrots);
$this->em->flush();
```The result after flush will generate the food tree:
```
food (1-8)
/fruits (2-3)
/vegetables (4-7)
/carrots (5-6)
```### Inserting node in different positions
```php
setTitle('Food');$fruits = new Category();
$fruits->setTitle('Fruits');$vegetables = new Category();
$vegetables->setTitle('Vegetables');$carrots = new Category();
$carrots->setTitle('Carrots');$treeRepository
->persistAsFirstChild($food)
->persistAsFirstChildOf($fruits, $food)
->persistAsLastChildOf($vegetables, $food)
->persistAsNextSiblingOf($carrots, $fruits);$em->flush();
```### Using repository functions
```php
getRepository('Entity\Category');$food = $repo->findOneByTitle('Food');
echo $repo->childCount($food);
// prints: 3
echo $repo->childCount($food, true/*direct*/);
// prints: 2
$children = $repo->children($food);
// $children contains:
// 3 nodes
$children = $repo->children($food, false, 'title');
// will sort the children by title
$carrots = $repo->findOneByTitle('Carrots');
$path = $repo->getPath($carrots);
/* $path contains:
0 => Food
1 => Vegetables
2 => Carrots
*/// verification and recovery of tree
$repo->verify();
// can return TRUE if tree is valid, or array of errors found on tree
$repo->recover();
$em->flush(); // important: flush recovered nodes
// if tree has errors it will try to fix all tree nodes// UNSAFE: be sure to backup before running this method when necessary, if you can use $em->remove($node);
// which would cascade to children
// single node removal
$vegies = $repo->findOneByTitle('Vegetables');
$repo->removeFromTree($vegies);
$em->clear(); // clear cached nodes
// it will remove this node from tree and reparent all children// reordering the tree
$food = $repo->findOneByTitle('Food');
$repo->reorder($food, 'title');
// it will reorder all "Food" tree node left-right values by the title
```For more examples and usage check original package documentation:
https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/tree.md