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

Doctrine Manager Laravel Package

daviddel/doctrine-manager

Laravel package to manage Doctrine ORM/DBAL inside your app. Provides a manager/service integration for setting up and accessing Doctrine connections and entity managers through Laravel’s container and config, aiming to simplify Doctrine usage alongside Eloquent.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require daviddel/doctrine-manager
    

    Add to config/app.php under providers:

    Daviddel\DoctrineManager\DoctrineManagerServiceProvider::class,
    
  2. Publish Config (if needed):

    php artisan vendor:publish --provider="Daviddel\DoctrineManager\DoctrineManagerServiceProvider"
    

    Config file: config/doctrine-manager.php.

  3. Basic Usage: Register a Doctrine entity manager in Laravel's IoC container:

    // In a service provider (e.g., AppServiceProvider)
    $this->app->singleton('doctrine.manager', function ($app) {
        return DoctrineManager::getManager();
    });
    
  4. First Use Case: Fetch an entity manager in a controller or service:

    use Daviddel\DoctrineManager\Facades\DoctrineManager;
    
    public function index()
    {
        $em = DoctrineManager::getManager();
        $users = $em->getRepository('App\Entity\User')->findAll();
        return view('users.index', compact('users'));
    }
    

Implementation Patterns

Workflows

  1. Entity Management:

    • CRUD Operations:
      $user = $em->getRepository('App\Entity\User')->find(1);
      $em->persist($newUser);
      $em->flush();
      
    • Bulk Operations:
      $query = $em->createQuery('SELECT u FROM App\Entity\User u WHERE u.active = :active')
          ->setParameter('active', true);
      $activeUsers = $query->getResult();
      
  2. Integration with Laravel:

    • Service Layer: Inject the manager into services:
      public function __construct(DoctrineManager $manager) {
          $this->manager = $manager;
      }
      
    • Repositories: Create custom repositories extending Doctrine\ORM\EntityRepository and bind them:
      $this->app->bind('App\Repositories\UserRepository', function ($app) {
          return $app['doctrine.manager']->getRepository('App\Entity\User');
      });
      
  3. Query Building:

    • Use DQL (Doctrine Query Language) for complex queries:
      $query = $em->createQuery('UPDATE App\Entity\User u SET u.lastLogin = :now WHERE u.id = :id');
      $query->execute(['now' => new \DateTime(), 'id' => 1]);
      
  4. Transactions: Wrap operations in transactions:

    $em->beginTransaction();
    try {
        $em->persist($entity);
        $em->flush();
        $em->commit();
    } catch (\Exception $e) {
        $em->rollback();
        throw $e;
    }
    

Gotchas and Tips

Pitfalls

  1. Outdated Package:

    • Last release in 2016 may cause compatibility issues with modern Laravel (8+) or Doctrine (2.7+).
    • Mitigation: Fork the repo and update dependencies (e.g., doctrine/orm, doctrine/common) to match your project.
  2. Configuration Overrides:

    • The package assumes Doctrine is configured via config/doctrine-manager.php. If using Laravel's built-in Doctrine integration (e.g., laravel-doctrine/orm), conflicts may arise.
    • Tip: Disable Laravel's Doctrine provider if using this package exclusively.
  3. Entity Namespace:

    • Ensure entity namespaces in queries (e.g., App\Entity\User) match your actual namespace. Misconfigurations lead to ClassNotFoundException.
    • Debug Tip: Use DoctrineManager::getManager()->getConfiguration()->getEntityNamespace() to verify.
  4. Singleton Manager:

    • The manager is a singleton by default. For multi-tenancy or testing, manually create new managers:
      $customManager = DoctrineManager::createManager(['driver' => 'your_config']);
      

Debugging

  1. Connection Issues:

    • Check config/doctrine-manager.php for correct connection and driver settings.
    • Enable Doctrine logging:
      $em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
      
  2. Lazy-Loading:

    • Proxy classes may not regenerate after changes. Clear cache:
      php artisan doctrine:cache:clear-metadata
      php artisan doctrine:cache:clear-query
      

Extension Points

  1. Custom Event Listeners: Add listeners via config:

    'listeners' => [
        'App\Events\UserListener',
    ],
    

    Or programmatically:

    $em->getEventManager()->addEventListener([/* ... */]);
    
  2. Custom Drivers: Extend the package to support non-Doctrine databases (e.g., MongoDB) by implementing Daviddel\DoctrineManager\Contracts\DriverInterface.

  3. Laravel Events: Trigger Laravel events from Doctrine lifecycle callbacks:

    $em->getEventManager()->addEventListener(
        \Doctrine\ORM\Events::postPersist,
        function ($event) {
            event(new \App\Events\UserCreated($event->getObject()));
        }
    );
    

Performance Tips

  1. Batch Processing: Use Doctrine\ORM\UnitOfWork for bulk inserts/updates:

    $uow = $em->getUnitOfWork();
    foreach ($users as $user) {
        $uow->computeChangeSet($em->getClassMetadata('App\Entity\User'), $user);
    }
    $em->flush();
    
  2. Query Caching: Enable second-level cache in config/doctrine-manager.php:

    'cache' => [
        'driver' => 'apcu',
        'entity_region' => 'default',
        'query_region' => 'default',
    ],
    
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.
croct/coding-standard
croct/plug-php
nqxcode/phpmorphy
boundwize/pyrameter
testo/facade
headercat/phpstan-extension-ide-helper
yosymfony/parser-utils
innmind/black-box
babenkoivan/elastic-migrations
babenkoivan/elastic-adapter
develia/commons
dmstr/symfony-system-resources-bundle
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
renatomarinho/laravel-page-speed
develia/geo-bundle
austinheap/laravel-database-encryption
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle