pvsaintpe/ltree-bundle

Symfony 4 bundle for postgresql ltee extension

Installs: 186

Dependents: 0

Suggesters: 0

Security: 0

Stars: 10

Watchers: 3

Forks: 5

Open Issues: 2

Type:symfony-bundle

2.0.1 2021-03-03 12:00 UTC

This package is auto-updated.

Last update: 2024-04-29 04:03:21 UTC


README

Installation:

composer require pvsaintpe/ltree-bundle

Using

  1. Create Entity class:
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Entity;
use LTree\Annotation\LTreeChilds;
use LTree\Annotation\LTreeEntity;
use LTree\Annotation\LTreeParent;
use LTree\Annotation\LTreePath;
use LTree\Repository\LTreeEntityInterface;

/**
 * Class TestEntity
 * @package LTree\Entity
 *
 * @Entity(repositoryClass="LTree\Entity\TestRepository")
 * @LTreeEntity()
 */
class TestEntity implements LTreeEntityInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @LTreePath()
     * @ORM\Column(type="ltree")
     */
    private $path = null;

    /**
     * @LTreeParent()
     * @ORM\ManyToOne(targetEntity="TestEntity", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;

    /**
     * @LTreeChilds()
     * @ORM\OneToMany(targetEntity="TestEntity", mappedBy="parent", cascade={"all"}, orphanRemoval=true)
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $children;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->children = new ArrayCollection();
    }
}
  1. Create Repository class:
use Doctrine\ORM\EntityManagerInterface;
use LTree\Repository\LTreeEntityRepository;

/**
 * Class TestRepository
 *
 * @method TestEntity|null find($id, $lockMode = null, $lockVersion = null)
 * @method TestEntity|null findOneBy(array $criteria, array $orderBy = null)
 * @method TestEntity[]    findAll()
 * @method TestEntity[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 *
 * @package LTree\Entity
 */
class TestRepository extends LTreeEntityRepository
{
    /**
     * TestRepository constructor.
     * @param EntityManagerInterface $registry
     */
    public function __construct(EntityManagerInterface $registry)
    {
        parent::__construct($registry, $registry->getClassMetadata(TestEntity::class));
    }
}
  1. Create Extension via migration
    public function up(Schema $schema) : void
    {
        $this->addSql('CREATE IF NOT EXISTS EXTENSION ltree');
    }
    ...
  1. Configure Doctrine Type via config (packages/doctrine.yaml):
doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        types:
            ltree:  LTree\Types\LTreeType
  1. Configure Bundle via config (bundles.php):
LTree\LTreeExtensionBundle::class => ['all' => true],