Doctrine Behaviors

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

It currently handles:

Blameable Loggable Sluggable SoftDeletable Uuidable Timestampable Translatable Tree

Install

composer require knplabs/doctrine-behaviors

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 AppRepository; use DoctrineORMEntityRepository; use KnpDoctrineBehaviorsORMTreeTreeTrait; final class CategoryRepository extends EntityRepository { use TreeTrait; }

Voilà!

You now have a working Category that behaves like.

PHPStan

A PHPStan extension is available and provides the following features:

Provides correct return type for TranslatableInterface::getTranslations() and TranslatableInterface::getNewTranslations() Provides correct return type for TranslatableInterface::translate() Provides correct return type for TranslationInterface::getTranslatable()

Include phpstan-extension.neon in your project's PHPStan config:

# phpstan.neon includes: - vendor/knplabs/doctrine-behaviors/phpstan-extension.neon

3 Steps to Contribute

1 feature per pull-request

New feature needs tests

Tests and static analysis must pass:

vendor/bin/phpunit composer fix-cs composer phpstan

Upgrade 1.x to 2

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

Create rector.php config:

vendor/bin/rector init

Add Doctrine Behaviors upgrade set to rector.php:

use RectorCoreConfigurationOption; use SymfonyComponentDependencyInjectionLoaderConfiguratorContainerConfigurator; use RectorDoctrineSetDoctrineSetList; return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(DoctrineSetList::DOCTRINE_BEHAVIORS_20); };

Run Rector:

vendor/bin/rector process src

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。