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.
composer require setono/doctrine-orm-traitUse 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.
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-stringCall 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();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']);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();