doctrine/persistence
Doctrine Persistence provides shared abstractions for persistence and object mappers in the Doctrine ecosystem. It defines common interfaces and utilities used by Doctrine ORM and related libraries to manage mapping, metadata, and repository behavior across storage backends.
ObjectManager, EntityManager, ClassMetadata) for ORM/ODM persistence, making it ideal for Laravel applications relying on Doctrine ORM (e.g., via doctrine/orm or doctrine/dbal). It decouples persistence logic from concrete implementations (e.g., Doctrine ORM, MongoDB ODM), enabling multi-persistence support in a Laravel monolith or microservices.laravel-doctrine/orm) or custom repositories, offering a unified persistence API for hybrid architectures. Key interfaces like ObjectRepository align with Laravel’s repository pattern.doctrine/orm (e.g., for complex queries or legacy systems), this package is a prerequisite for EntityManager functionality. Laravel’s Eloquent cannot directly use this package, but a wrapper layer (e.g., a custom DoctrineObjectManager service) could bridge the gap.Repository interfaces (e.g., BaseRepository) can leverage ObjectRepository from this package, reducing boilerplate for CRUD operations. Example:
class UserRepository implements ObjectRepository {
use Doctrine\Persistence\ObjectRepositoryTrait;
// Laravel-specific logic
}
LifecycleEventArgs) can be adapted for Laravel’s Model events (e.g., saved, deleted) via event dispatchers.StaticReflectionService, PHP 8.1+ requirement). A feature freeze on v3.4.x could mitigate this.ObjectManager in Laravel’s PHPUnit tests requires custom test doubles due to Doctrine’s tight coupling with PSR-11 containers.ObjectRepository? Will a hybrid layer be needed?laravel-doctrine/orm) or requiring standardized repository interfaces across multiple persistence layers (e.g., SQL + NoSQL).| Step | Action | Tools/Dependencies | Risk |
|---|---|---|---|
| 1 | Assess Scope | Audit existing Eloquent repositories. Identify pain points (e.g., complex joins, bulk updates). | Low |
| 2 | Add Doctrine ORM | Install doctrine/orm, doctrine/persistence, and laravel-doctrine/orm. Configure config/doctrine.php. |
Medium (DBAL schema changes) |
| 3 | Hybrid Repository Layer | Create abstract DoctrineRepository extending ObjectRepositoryTrait and implement Laravel-specific methods. |
Medium (Design complexity) |
| 4 | Incremental Replacement | Migrate one repository at a time. Use feature flags to toggle between Eloquent and Doctrine. | High (Testing effort) |
| 5 | Event Bridge | Map Doctrine events (prePersist) to Laravel events (creating) via listeners. |
Low |
| 6 | Performance Tuning | Enable Doctrine’s metadata cache (APCu/Redis) and query caching. | Low |
ObjectManager:
$this->app->bind(\Doctrine\Persistence\ObjectManager::class, function ($app) {
return $app->make(\Doctrine\ORM\EntityManager::class);
});
@ORM\Table).doctrine/orm’s EntityManager in tests or mock ObjectRepository with Mockery:
$mockRepo = Mockery::mock(\Doctrine\Persistence\ObjectRepository::class);
$mockRepo->shouldReceive('find')->andReturn($user);
ObjectRepository implementations.@ORM\Id) may surface in Laravel’s logs, requiring custom error handlers.php artisan commands (e.g., migrate) may need Doctrine-specific extensions (e.g., doctrine:schema:update).laravel-doctrine/orm GitHub issues.EntityManager is thread-safe (unlike Eloquent’s singleton), but Laravel’s queue workers may need per-request EntityManager instances to avoid state leaks.FetchMode::SELECT_IN), but requires developer discipline.| Scenario | Impact | Mitigation |
|---|---|---|
| Metadata Cache Corruption | ClassMetadata errors crash queries. |
Use Redis cache with fallback to filesystem. |
| Transaction Deadlocks | Doctrine’s EntityManager locks tables longer than Eloquent. |
Set transaction-isolation in DB config. |
| PHP 8.1+ Breaking Changes | v4.x drops PHP 7.4 support. | Pin to ^3.4 until Laravel 10 adoption. |
| Laravel Cache Invalidation | Doctrine’s metadata cache conflicts with Laravel’s cache. | Exclude Doctrine cache |
How can I help you explore Laravel packages today?