Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Persistence Laravel Package

doctrine/persistence

Leverage Doctrine Persistence for consistent object mapping and database interactions in PHP. Abstracts ORM logic, simplifying persistence across frameworks. Supports Doctrine ORM, DBAL, and more with unified abstractions. Ideal for scalable applications needing clean persistence patterns.

View on GitHub
Deep Wiki
Context7

Getting Started

The doctrine/persistence package is not typically used directly by application developers—it’s a foundational abstraction layer consumed by higher-level libraries like doctrine/orm, doctrine/mongodb-odm, or custom object mappers. As a developer, you’ll most often interact with it indirectly when configuring or extending Doctrine ORM/ODM, or when building your own persistence layer. Start by examining the interface contracts (ObjectManager, ObjectRepository, ClassMetadata) and lifecycle event abstractions (LifecycleEventArgs, LifecycleCallback). For quick local exploration, run composer require doctrine/persistence and inspect the src/Persistence directory structure—especially Events.php, Mapping/AbstractClassMetadataFactory.php, and ObjectManagerDecorator.php.

Implementation Patterns

  • ObjectManager Decoration: Extend ObjectManagerDecorator to intercept and add cross-cutting concerns (e.g., auditing, caching) around entity operations without modifying the underlying manager (e.g., EntityManager).
  • Custom Metadata Drivers: Implement Driver interfaces from Persistence\Mapping\Driver (e.g., XmlDriver, YamlDriver, PHPDriver) or extend AbstractFileDriver to support configuration in non-Doctrine formats (e.g., YAML in legacy systems).
  • Class Metadata Loading: Use AbstractClassMetadataFactory as a base to implement custom metadata loading strategies (e.g., database-backed, API-driven). The new ClassLocator (4.1.0+) simplifies discovering entity classes for attribute-based drivers.
  • Event Listeners/Subscribers: Hook into Doctrine lifecycle events using LifecycleEventArgs and EventManager—often via decorators or custom ObjectManager implementations.
  • Proxy Support: Leverage RuntimeReflectionProperty and Proxy utilities when writing custom proxy generators or lazy-loading mechanisms.

Gotchas and Tips

  • BC Breaks Across Versions: Version 3.x removed ObjectManagerAware, doctrine/cache support, and clear($className); version 4.x requires PHP 8.1+. Ensure your drivers/listeners are version-compatible—check release notes before upgrading.
  • Anonymous Classes: Older versions failed with PHP’s anonymous classes; patch-level versions (e.g., 2.5.2, 3.0.1) added graceful handling. Use ClassMetadata::getReflectionClass() instead of new \ReflectionClass() for robustness.
  • Class Name Normalization: Leading backslashes and case sensitivity can bite you—especially in ODMs or multi-DB setups. Versions 2.5.4/3.0.3+ normalize class names; avoid raw string comparisons.
  • Template Variance: ObjectRepository’s template parameter is covariant—be precise with return types in custom repository implementations to avoid PHPStan/Psalm errors.
  • Enum Handling: The package has first-class support for enums (EnumReflectionProperty, setValue()), but ensure you’re on ≥3.3.0 to use array-of-enums and enum arguments reliably.
  • No Direct Cache: Don’t expect built-in caching—caching is delegated to consumers (e.g., Doctrine ORM uses doctrine/cache separately). If writing a driver, always check isTransient() before attempting to load metadata.
  • Testing Tip: Use ObjectManagerDecorator or ObjectRepositoryDecorator as test doubles to stub only the methods you need—don’t mock the entire ObjectManager.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport