nitra/doctrine-behaviors
PHP 5.4+ trait-based behaviors for Doctrine2 entities and repositories: tree, translatable, timestampable, soft deletable, blameable, loggable, geocodable, filterable, and sluggable. Includes optional Doctrine event listeners for behavior support.
Pros:
Tree, Translatable, SoftDeletable), reducing boilerplate and enforcing consistency.getSluggableFields(), getRegenerateSlugOnUpdate()) and callables for dynamic behavior (e.g., user resolution in Blameable).Cons:
Translatable) require Doctrine annotations, which may conflict with Laravel’s attribute-based mappings or annotation-free approaches.Translatable or Blameable mandate listener registration, adding complexity to entity lifecycle management.Geocodable relies on PostgreSQL-specific extensions (e.g., cube, earthdistance), restricting database compatibility.Laravel Compatibility:
doctrine/orm or laravel-doctrine. This package would require Doctrine ORM to be the primary ORM, which may not align with Laravel’s default stack.NodeInterface) may need adapters or wrappers to work with Laravel’s conventions (e.g., Illuminate\Database\Eloquent\Model).Illuminate\Support\Facades\Event) or Doctrine’s event manager.Key Challenges:
#[ORM\...]) may conflict with annotation-based behaviors.Geocodable) would require custom database drivers or alternative implementations (e.g., using Laravel’s spatie/laravel-geocoder).High:
Tree, Translatable) introduce query complexity or additional database operations.Mitigation Strategies:
Geocodable) while keeping Eloquent for core models.LaravelTreeNode) to abstract Doctrine dependencies.Sluggable via a standalone package like spatie/laravel-sluggable instead).Why Doctrine ORM?
Behavior Prioritization:
SoftDeletable, Translatable) vs. optional (e.g., Geocodable)?spatie/laravel-translatable, nWidart/laravel-modules)?Database Compatibility:
Geocodable be replaced with a Laravel-compatible geospatial solution (e.g., spatie/laravel-geocoder)?Long-Term Maintenance:
Performance Impact:
Tree or Translatable been benchmarked in a Laravel context?Translatable translations) to mitigate performance costs?Primary Fit:
Tree), multilingual support (Translatable), or audit trails (Blameable).Secondary Fit:
Geocodable and other PostgreSQL-dependent behaviors align well with PostgreSQL-centric Laravel apps.Poor Fit:
Geocodable or SoftDeletable may not work across MySQL, SQLite, etc., without significant adaptation.Assessment Phase:
Tree queries, Translatable joins).Pilot Implementation:
Sluggable, Timestampable) in a single module.// app/Models/Doctrine/Category.php (Doctrine entity)
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
class Category { use ORMBehaviors\Tree\Node; }
// app/Models/Eloquent/Product.php (Eloquent model)
// No Doctrine behaviors; use native Laravel features.
Listener Integration:
Translatable:
// app/Providers/DoctrineServiceProvider.php
use Doctrine\ORM\Events;
use Knp\DoctrineBehaviors\ORM\Translatable\TranslatableListener;
public function register()
{
$em = DoctrineHelper::getEntityManager();
$em->getEventManager()->addEventSubscriber(new TranslatableListener());
}
Database Schema Adaptation:
deleted_at, created_at, slug) to existing tables.Geocodable, ensure PostgreSQL extensions are enabled and create custom database views or functions if needed.Testing and Validation:
SoftDeletable soft deletes, Tree hierarchy queries).Blameable, locale switching with Translatable).Doctrine ORM:
doctrine/orm:^2.10).orm-services.yml to Laravel’s service container (e.g., using config/doctrine.php).Laravel-Specific:
@ORM\ annotations with Laravel attributes or configure Doctrine to parse annotations in Laravel’s app/ directory.Blameable user resolver).deleted_at, slug).Third-Party Dependencies:
Geocodable, integrate willdurand/Geocoder as a Laravel service.Loggable, use Laravel’s logging system (e.g., Log::channel('doctrine')).How can I help you explore Laravel packages today?