knplabs/doctrine-behaviors

Doctrine Behavior Traits


README

Build Status Downloads

This PHP library is a collection of traits and interfaces that add behaviors to Doctrine entities and repositories.

It currently handles:

Install

composer require knplabs/doctrine-behaviors

Register bundle in AppKernel:

<?php

declare(strict_types=1);

namespace App;

use Symfony\Component\HttpKernel\Kernel;

final class AppKernel extends Kernel
{
    public function registerBundles(): array
    {
        return [
            //...
            new Knp\DoctrineBehaviors\Bundle\DoctrineBehaviorsBundle(),
            //...
        ];
    }
}

Usage

All you have to do is to define a Doctrine entity:

  • implemented interface
  • add a trait

For some behaviors like tree, you can use repository traits:

<?php

declare(strict_types=1);

namespace App\Repository;  

use Doctrine\ORM\EntityRepository;
use Knp\DoctrineBehaviors\ORM\Tree\TreeTrait;

final class CategoryRepository extends EntityRepository
{
    use TreeTrait;
}

Voilá!

You now have a working Category that behaves like:

Testing

vendor/bin/phpunit

Run coding standard fixes and check static analysis:

composer fix-cs
composer phpstan