setono / doctrine-orm-trait
A very simple library that offers a trait to get the object manager and repository for a given class
Fund package maintenance!
Requires
- php: >=8.1
- doctrine/orm: ^2.8 || ^3.1
- doctrine/persistence: ^1.3 || ^2.5 || ^3.1 || ^4.1
Requires (Dev)
- ergebnis/composer-normalize: ^2.52
- infection/infection: ^0.27.11
- jangregor/phpstan-prophecy: ^2.3
- phpspec/prophecy: ^1.20
- phpspec/prophecy-phpunit: ^2.5
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.2
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpstan/phpstan-webmozart-assert: ^2.0
- phpunit/phpunit: ^10.5
- rector/rector: ^2.5
- shipmonk/composer-dependency-analyser: ^1.8
- sylius-labs/coding-standard: ^4.5
This package is auto-updated.
Last update: 2026-06-22 12:24:26 UTC
README
A tiny, dependency-light trait that gives your services convenient, type-safe access to Doctrine's entity managers and repositories.
It's built for the common pattern of injecting the ManagerRegistry instead of an EntityManager
directly:
once you have the registry, this trait does the work of resolving the right manager and repository for
a given entity — including the static type juggling — so you don't have to repeat it in every service.
Installation
composer require setono/doctrine-orm-trait
Usage
Use the trait and assign the injected ManagerRegistry to the $managerRegistry property in your
constructor. That's the only wiring required:
<?php use Doctrine\Persistence\ManagerRegistry; use Setono\Doctrine\ORMTrait; final class OrderProcessor { use ORMTrait; public function __construct(ManagerRegistry $managerRegistry) { $this->managerRegistry = $managerRegistry; } public function process(Order $order): void { $manager = $this->getManager($order); $manager->persist($order); $manager->flush(); } }
The trait exposes two protected methods.
getManager() — resolve the entity manager
Pass an entity instance or a class-string to get the manager responsible for that entity. Resolved managers are memoized, so repeated calls are cheap:
$manager = $this->getManager($order); // from an instance $manager = $this->getManager(Order::class); // from a class-string
Call it without arguments to get the default entity manager. If more than one manager is registered,
this throws an \InvalidArgumentException — pass an entity instead so the trait knows which one you mean:
$manager = $this->getManager();
getRepository() — resolve the repository
Like getManager(), this accepts an entity instance or a class-string and returns the matching
EntityRepository:
$repository = $this->getRepository(Order::class); $orders = $repository->findBy(['status' => 'pending']);
Typed repositories
If you have a custom repository class, pass it as the second argument. The trait asserts the repository
is an instance of that type (throwing an \InvalidArgumentException otherwise) and narrows the
return type for static analysis, so your IDE and PHPStan know about the repository's own methods:
// $repository is statically typed as OrderRepository $repository = $this->getRepository(Order::class, OrderRepository::class); $orders = $repository->findPendingOrders();