byscripts/object-manager-bundle
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],
];
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());
}
}
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();
Dependency Injection
Prefer constructor injection for ObjectManager over service locator pattern:
public function __construct(ObjectManager $manager) {}
Repository Abstraction
Use the ObjectManager to fetch repositories dynamically:
$userRepo = $this->objectManager->getRepository('App\Entity\User');
$users = $userRepo->findAll();
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;
}
Event Listeners
Attach listeners to the ObjectManager for lifecycle events:
$this->objectManager->addEventListener('prePersist', function ($event) {
$entity = $event->getEntity();
$entity->setCreatedAt(new \DateTime());
});
Batch Operations Use bulk methods for efficiency:
$this->objectManager->bulkUpdate('App\Entity\User', ['active' => false], ['id' => [1, 2, 3]]);
Deprecated API The package is abandoned (last release 2014). Avoid in production; prefer modern alternatives like:
Doctrine\ORM\EntityManagerInterfaceApiPlatform\State\ProcessorNo Doctrine 2.10+ Support
May fail with newer Doctrine versions due to deprecated methods (e.g., find() instead of findOneBy()).
No DDD/ODM Integration Limited support for Doctrine ODM (MongoDB) or Domain-Driven Design patterns.
Thread Safety
The ObjectManager is not thread-safe. Avoid in multi-threaded contexts (e.g., Symfony Messenger workers).
Enable Doctrine Logging
Add to config/packages/dev/doctrine.yaml:
doctrine:
orm:
logging: true
logging:
enabled: true
filter: true
sql_comment: true
format: '%%sql%%'
Check for Deprecated Calls Use PHPStan or Psalm to detect unsupported methods:
vendor/bin/phpstan analyse --level 7
Fallback to EntityManager
If the ObjectManager fails, inject EntityManagerInterface as a secondary dependency:
public function __construct(
ObjectManager $objectManager,
EntityManagerInterface $em
) {
$this->manager = $objectManager ?: $em;
}
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();
}
}
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());
}
});
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).
How can I help you explore Laravel packages today?