tourze / doctrine-helper
Doctrine Helper
Installs: 27 786
Dependents: 17
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/doctrine-helper
Requires
- php: ^8.1
- doctrine/common: ^3.5
- doctrine/orm: ^3.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-10-31 07:30:01 UTC
README
A lightweight helper library to simplify and optimize your usage of Doctrine ORM in PHP projects.
Table of Contents
Features
- CacheHelper: Generate cache IDs and tags for Doctrine entities and objects with table name support
- EntityDetector: Determine whether a class is a Doctrine entity using attributes
- ReflectionHelper: Simplify reflection operations for Doctrine entities, properties, and custom attributes, with built-in caching for performance
- SortableTrait: Add sorting functionality to Doctrine entities with a simple trait
Requirements
- PHP >= 8.1
- doctrine/orm >= 3.0
- doctrine/common >= 3.5
- doctrine/dbal >= 4.0
Installation
composer require tourze/doctrine-helper
Quick Start
CacheHelper Example
use Tourze\DoctrineHelper\CacheHelper; // Get cache ID for a class $cacheId = CacheHelper::getClassId(User::class); // Get cache tags for a class $tags = CacheHelper::getClassTags(User::class); // Get cache tags for a class with ID $tagsWithId = CacheHelper::getClassTags(User::class, '1'); // Get cache tags for an object $objectTags = CacheHelper::getObjectTags($user);
EntityDetector Example
use Tourze\DoctrineHelper\EntityDetector; // Check if a class is a Doctrine entity if (EntityDetector::isEntityClass(User::class)) { // This is a Doctrine entity }
ReflectionHelper Example
use Tourze\DoctrineHelper\ReflectionHelper; // Get class reflection $reflection = ReflectionHelper::getClassReflection(User::class); // Get property reflection $property = ReflectionHelper::getReflectionProperty($user, 'email'); // Get all properties $properties = ReflectionHelper::getProperties(User::class); // Get all methods $methods = ReflectionHelper::getMethods(User::class); // Get property attributes $attributes = ReflectionHelper::getPropertyAttributes($reflection, SomeAttribute::class); // Get class attributes $classAttributes = ReflectionHelper::getClassAttributes($reflection, SomeAttribute::class); // Check if class has specific attributes $hasAttribute = ReflectionHelper::hasClassAttributes($reflection, SomeAttribute::class);
SortableTrait Example
use Doctrine\ORM\Mapping as ORM; use Tourze\DoctrineHelper\SortableTrait; #[ORM\Entity] class Product { use SortableTrait; // Your entity properties here } // Usage $product = new Product(); $product->setSortNumber(10); $sortNumber = $product->getSortNumber(); $sortableArray = $product->retrieveSortableArray();
Advanced Usage
Performance Features
- Reflection caching: All reflection operations are cached internally for better performance
- Attribute reading: Full support for PHP 8+ attributes
- Memory efficient: Optimized for minimal memory usage
Working with Attributes
The ReflectionHelper provides powerful methods for working with PHP 8+ attributes:
use Tourze\DoctrineHelper\ReflectionHelper; $reflection = ReflectionHelper::getClassReflection(MyEntity::class); // Get all instances of a specific attribute from properties $attributes = ReflectionHelper::getPropertyAttributes($reflection, MyAttribute::class); // Get first instance of a specific attribute from properties $attribute = ReflectionHelper::getPropertyAttribute($reflection, MyAttribute::class); // Iterate through class attributes foreach (ReflectionHelper::getClassAttributes($reflection, MyAttribute::class) as $attr) { // Process each attribute instance } // Check if class has specific attributes if (ReflectionHelper::hasClassAttributes($reflection, MyAttribute::class)) { // Handle accordingly }
Cache Management
CacheHelper provides flexible caching strategies:
use Tourze\DoctrineHelper\CacheHelper; // Generate stable cache keys $key = CacheHelper::getClassId(User::class); // Returns: 'User' // Generate cache tags for invalidation $tags = CacheHelper::getClassTags(User::class); // Returns: ['User'] $tags = CacheHelper::getClassTags(User::class, '123'); // Returns: ['User', 'User:123'] // Get tags from entity instances $user = new User(); $user->setId(456); $tags = CacheHelper::getObjectTags($user); // Returns: ['User', 'User:456']
API Reference
CacheHelper
getClassId(string $className): stringgetClassTags(string $className, string $id = null): arraygetObjectTags(object $object): array
EntityDetector
isEntityClass(string $className): bool
ReflectionHelper
getClassReflection(object|string $object): \ReflectionClassgetReflectionProperty(object $object, string $propertyName): ?\ReflectionPropertygetProperties(object|string $object, $filter = null): arraygetMethods(object|string $object, $filter = null): arraygetParentClasses(\ReflectionClass $reflectionClass): arraygetPropertyAttributes(\ReflectionClass $reflectionClass, string $attributeName): arraygetPropertyAttribute(\ReflectionClass $reflectionClass, string $attributeName): ?objectgetClassAttributes(\ReflectionClass $reflectionClass, string $attributeName): \TraversablehasClassAttributes(\ReflectionClass $reflectionClass, string $attributeName): bool
SortableTrait
setSortNumber(int $sortNumber): selfgetSortNumber(): intretrieveSortableArray(): array
Contributing
- Issues and pull requests are welcome.
- Follow PSR-12 coding style.
- Ensure all PHPUnit tests pass before submitting.
License
MIT License. See LICENSE for details.
Changelog
See Git commit history for changes and releases.