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

Object Manager Bundle Laravel Package

byscripts/object-manager-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require byscripts/object-manager-bundle
    

    Register the bundle in config/bundles.php (Symfony 4+):

    return [
        // ...
        Byscripts\ObjectManagerBundle\ByscriptsObjectManagerBundle::class => ['all' => true],
    ];
    
  2. Basic Usage Inject the ObjectManager service into your controller/service:

    use Byscripts\ObjectManagerBundle\Manager\ObjectManager;
    
    class MyController extends AbstractController
    {
        public function __construct(private ObjectManager $objectManager) {}
    
        public function index()
        {
            $user = $this->objectManager->find('App\Entity\User', 1);
            return new Response($user->getName());
        }
    }
    
  3. First Use Case Replace direct Doctrine EntityManager calls with the ObjectManager for CRUD operations:

    // Instead of:
    $em->persist($user)->flush();
    
    // Use:
    $this->objectManager->persist($user);
    $this->objectManager->flush();
    

Implementation Patterns

Workflow Integration

  1. Dependency Injection Prefer constructor injection for ObjectManager over service locator pattern:

    public function __construct(ObjectManager $manager) {}
    
  2. Repository Abstraction Use the ObjectManager to fetch repositories dynamically:

    $userRepo = $this->objectManager->getRepository('App\Entity\User');
    $users = $userRepo->findAll();
    
  3. Transaction Management Wrap operations in transactions:

    $this->objectManager->beginTransaction();
    try {
        $this->objectManager->persist($entity);
        $this->objectManager->flush();
        $this->objectManager->commit();
    } catch (\Exception $e) {
        $this->objectManager->rollback();
        throw $e;
    }
    
  4. Event Listeners Attach listeners to the ObjectManager for lifecycle events:

    $this->objectManager->addEventListener('prePersist', function ($event) {
        $entity = $event->getEntity();
        $entity->setCreatedAt(new \DateTime());
    });
    
  5. Batch Operations Use bulk methods for efficiency:

    $this->objectManager->bulkUpdate('App\Entity\User', ['active' => false], ['id' => [1, 2, 3]]);
    

Gotchas and Tips

Pitfalls

  1. Deprecated API The package is abandoned (last release 2014). Avoid in production; prefer modern alternatives like:

  2. No Doctrine 2.10+ Support May fail with newer Doctrine versions due to deprecated methods (e.g., find() instead of findOneBy()).

  3. No DDD/ODM Integration Limited support for Doctrine ODM (MongoDB) or Domain-Driven Design patterns.

  4. Thread Safety The ObjectManager is not thread-safe. Avoid in multi-threaded contexts (e.g., Symfony Messenger workers).


Debugging Tips

  1. Enable Doctrine Logging Add to config/packages/dev/doctrine.yaml:

    doctrine:
        orm:
            logging: true
            logging:
                enabled: true
                filter: true
                sql_comment: true
                format: '%%sql%%'
    
  2. Check for Deprecated Calls Use PHPStan or Psalm to detect unsupported methods:

    vendor/bin/phpstan analyse --level 7
    
  3. Fallback to EntityManager If the ObjectManager fails, inject EntityManagerInterface as a secondary dependency:

    public function __construct(
        ObjectManager $objectManager,
        EntityManagerInterface $em
    ) {
        $this->manager = $objectManager ?: $em;
    }
    

Extension Points

  1. Custom Object Managers Extend the base class to add domain-specific logic:

    class UserObjectManager extends ObjectManager
    {
        public function findActiveUsers(): array
        {
            return $this->createQueryBuilder('u')
                ->where('u.active = :active')
                ->setParameter('active', true)
                ->getQuery()
                ->getResult();
        }
    }
    
  2. Event Subscribers Override event handling:

    $this->objectManager->addEventSubscriber(new class implements EventSubscriber {
        public function getSubscribedEvents(): array
        {
            return ['preUpdate'];
        }
    
        public function preUpdate(LifecycleEventArgs $args): void
        {
            $args->getObject()->setUpdatedAt(new \DateTime());
        }
    });
    
  3. Proxy Support If using proxies (e.g., for lazy-loading), ensure the ObjectManager is initialized early in the request lifecycle (e.g., via a compiler pass).

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.
craftcms/url-validator
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony