This PHP library is a collection of traits and interfaces that add behaviors to Doctrine entities and repositories.
It currently handles:
composer require knplabs/doctrine-behaviors
Register bundle in config/bundles.php
:
<?php
return [
// ...
Knp\DoctrineBehaviors\Bundle\DoctrineBehaviorsBundle::class => ['all' => true],
// ...
];
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:
-
1 feature per pull-request
-
New feature needs tests
-
Tests and static analysis must pass:
vendor/bin/phpunit composer fix-cs composer phpstan
There have been many changes between 1 and 2, but don't worry. This package uses Rector, that handles upgrade for you.
composer require rector/rector --dev
vendor/bin/rector process src --config vendor/knplabs/doctrine-behaviors/upgrade/rector/doctrine-behaviors-20.yaml