Skip to content

Setono/doctrine-orm-trait

Repository files navigation

Doctrine ORM Trait

Latest Version Software License Build Status Code Coverage Mutation testing

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();

About

A trait to get the object manager and repository for a given class if you're using Doctrine ORM

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages