doctrine/persistence
Doctrine Persistence provides shared interfaces and abstractions for object mapper persistence in PHP. It standardizes common concepts like object managers, repositories, and metadata across Doctrine and other mappers, helping libraries integrate consistently.
doctrine/dbal or doctrine/orm). It decouples persistence logic from specific implementations (e.g., SQL, NoSQL), enabling future-proofing for hybrid storage backends.illuminate/database bridges) or custom repositories, offering a more robust alternative for complex queries, caching, or multi-database setups.EventManager and LifecycleEventArgs align with Laravel’s event system, enabling seamless integration for pre/post-persistence hooks (e.g., persist, remove).ObjectManager, ObjectRepository, ClassMetadata) allow gradual adoption without rewriting existing Eloquent models. A TPM could:
Repository pattern.ObjectRepository::findBy()).StaticReflectionService removal in v4.0.0).doctrine/annotations or doctrine/orm for full functionality, adding ~5MB to the vendor directory. Justify via performance gains (e.g., DQL vs. Eloquent queries).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| BC Breaks in v4.0.0 | High | Pin to v3.4.x for stability; migrate incrementally. |
| ORM-Specific Quirks | Medium | Test with doctrine/orm first; abstract Laravel-specific logic. |
| Performance Overhead | Low | Benchmark against Eloquent for critical paths. |
| Learning Curve | Medium | Document mapping between Eloquent/Doctrine terms (e.g., Model ↔ Entity). |
ObjectManager) integrate with Laravel’s testing stack?doctrine/orm or illuminate/database bridges) or needing advanced persistence features (e.g., inheritance mapping, custom repositories).Repository pattern for read-heavy or analytical queries (e.g., DQL vs. Eloquent’s query builder).| Phase | Actionable Steps | Tools/Libraries |
|---|---|---|
| Assessment | Audit Eloquent usage; identify repetitive queries or N+1 problems. | Laravel Debugbar, Xdebug |
| Pilot | Replace 1–2 complex repositories with Doctrine equivalents. | doctrine/orm, doctrine/annotations |
| Hybrid Mode | Use Doctrine for reads, Eloquent for writes (via service layer). | Laravel Service Container |
| Full Migration | Gradually replace Eloquent models with Doctrine Entity classes. |
doctrine/migrations, rector |
| Optimization | Leverage DQL, second-level cache, or query batching. | Doctrine Cache, ObjectRepository |
Doctrine\ORM\Mapping\Entity instead of Illuminate\Database\Eloquent\Model.DoctrineMigrationsBundle alongside Laravel migrations.ObjectManager as a singleton:
$container->bind('doctrine.orm.entity_manager', function ($c) {
return ORM::buildEntityManager();
});
@ORM\Entity, @ORM\Table, etc. (or XML/YAML).proxy_dir in config/doctrine.yaml for lazy-loading.OnFlushEvent, PostPersistEvent via Laravel’s event system.doctrine/orm with a single entity.ObjectRepository.Repository pattern centralizes logic.@ORM\* annotations (or XML/YAML).proxy_dir to avoid ClassNotFoundException.fetch="EAGER" or @ORM\BackedEnum for enums.PARTIAL INDEX hints for large datasets.doctrine/cache) for ObjectRepository results.doctrine/dbal for multi-threaded workloads.Connection objects.doctrine/orm with custom routing (e.g., by tenant ID).flush() vs. Eloquent’s insert).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Database Schema Mismatch | Runtime MappingException |
Use doctrine/orm:schema-tool for validation. |
| Proxy Generation Failures | ClassNotFoundException |
Ensure proxy_dir is writable. |
| Transaction Deadlocks | App hangs on flush() |
Use retryOnDeadlock in DBAL. |
| **Memory |
How can I help you explore Laravel packages today?