- How does this bundle differ from Laravel Eloquent’s built-in `morphTo` for polymorphic relationships?
- This bundle uses PHP closures and Doctrine ORM for lazy-loaded polymorphic references, which can defer hydration until needed. Eloquent’s `morphTo` is simpler but doesn’t support closure-based lazy loading or Doctrine’s advanced features like attribute-based mapping. Choose this if you need closure-based flexibility or are already using Doctrine.
- Can I use this bundle in a pure Laravel/Eloquent app without Doctrine ORM?
- No, this bundle requires Doctrine ORM (v2.5+) and Symfony FrameworkBundle, which aren’t native to Laravel. You’d need to integrate Doctrine alongside Eloquent, which may introduce conflicts in service containers or hydration strategies. Consider alternatives like `spatie/laravel-morph-many-to-many` if you want Eloquent-only solutions.
- What Laravel versions are supported, and do I need Symfony components?
- The bundle requires Symfony FrameworkBundle (v6.3/7.x) and Doctrine ORM, which aren’t natively supported in Laravel. You’ll need to manually bootstrap Symfony components (e.g., via `symfony/flex` or custom kernel setup) or use a Laravel kernel compatible with Symfony. Laravel 9+ is likely the minimum viable version due to PHP 8+ attribute support.
- How do I configure the bundle to work with Laravel’s service container?
- You’ll need to bind Doctrine’s `EntityManagerInterface` to Laravel’s container manually in a service provider. Example: `$this->app->singleton(EntityManagerInterface::class, fn($app) => DoctrineORMModule::configureEntityManager($app['config']['doctrine']));`. Ensure Doctrine entities are registered as Laravel services if you want dependency injection to work seamlessly.
- What are the risks of storing closures in the database for polymorphic references?
- Closures stored as strings in the DB can break if referenced classes aren’t autoloaded or if the closure’s scope changes. Serialization/deserialization may fail in edge cases (e.g., circular references). Test thoroughly in staging, and consider fallback strategies like UUID/foreign key patterns if reliability is critical.
- How do I migrate an existing Laravel/Eloquent app to use this bundle?
- Start by adding `actor_id` (string) and `actor_type` (string) columns to your tables. Backfill data via a migration, then replace Eloquent’s `belongsTo`/`morphTo` with the `#[ObjectReference]` attribute. Pilot with a single entity (e.g., `Story`) to test Doctrine + Eloquent coexistence before full migration.
- Will this bundle work with Laravel’s route/model binding (e.g., `Route::model()`)?
- Route/model binding may conflict with Doctrine’s hydration strategies, especially if both ORMs are active. Test binding Doctrine entities directly via Symfony’s routing system or use a hybrid approach where Doctrine-managed entities are also registered as Laravel services. Debugging may require custom compiler passes.
- Are there performance implications for using closure-based lazy loading?
- Yes, closures introduce runtime reflection overhead, which can impact high-throughput systems. Benchmark your use case (e.g., audit logs vs. real-time events) and compare against eager loading or UUID/foreign key patterns. Lazy loading is ideal for rarely accessed polymorphic fields but may not suit performance-critical paths.
- How does this bundle handle transactions when both Doctrine and Eloquent are active?
- Transactions are managed by the active ORM, but conflicts can arise if both Doctrine and Eloquent attempt to commit changes simultaneously. Use a single ORM for transactional operations or implement a custom transaction manager to coordinate between them. Test rollback scenarios for polymorphic references.
- What alternatives should I consider if this bundle isn’t a good fit?
- For Laravel/Eloquent-only apps, consider `spatie/laravel-morph-many-to-many` for complex polymorphic relationships or stick with Eloquent’s native `morphTo`. If you need Doctrine ORM, explore `doctrine/orm` directly or hybrid bundles like `laravel-doctrine/orm`. Avoid this bundle if you prioritize simplicity or don’t need closure-based lazy loading.