setono/doctrine-orm-trait

一个提供获取指定类对象管理器和仓库特性的非常简单的库

v1.1.0 2024-05-07 08:57 UTC

This package is auto-updated.

Last update: 2024-09-07 09:38:56 UTC


README

Latest Version Software License Build Status Code Coverage Mutation testing

如果你像我一样,通常不直接注入实体管理器,而是注入管理器注册表,那么这个小库将非常有用。点击这里了解更多

安装

composer require setono/doctrine-orm-trait

用法

<?php
use Doctrine\Persistence\ManagerRegistry;
use Setono\Doctrine\ORMTrait;

final class YourClass
{
    /**
     * Include this trait to use the getManager() and getRepository() methods below
     */
    use ORMTrait;
    
    public function __construct(ManagerRegistry $managerRegistry)
    {
        $this->managerRegistry = $managerRegistry;
    }
    
    public function someMethod(): void
    {
        /**
         * $entity<T> is an entity managed by Doctrine or a class-string representing an entity managed by Doctrine
         */
        $entity = ;
        
        /** @var \Doctrine\ORM\EntityRepository<T> $repository */
        $repository = $this->getRepository($entity);
        
        /**
         * @var \Doctrine\ORM\EntityManagerInterface $manager 
         */
        $manager = $this->getManager($entity);
        
        $manager->persist($entity);
        $manager->flush();
    }
}