setono / doctrine-orm-trait
一个提供获取指定类对象管理器和仓库特性的非常简单的库
v1.1.0
2024-05-07 08:57 UTC
Requires
- php: >=8.1
- doctrine/orm: ^2.8 || ^3.1
- doctrine/persistence: ^1.3 || ^2.5 || ^3.1
Requires (Dev)
- infection/infection: ^0.27
- phpspec/prophecy-phpunit: ^2.2
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.19
- setono/code-quality-pack: ^2.7
This package is auto-updated.
Last update: 2024-09-07 09:38:56 UTC
README
如果你像我一样,通常不直接注入实体管理器,而是注入管理器注册表,那么这个小库将非常有用。点击这里了解更多。
安装
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(); } }