setono/doctrine-orm-trait
Composer 安装命令:
composer require setono/doctrine-orm-trait
包简介
A very simple library that offers a trait to get the object manager and repository for a given class
README 文档
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();
setono/doctrine-orm-trait 适用场景与选型建议
setono/doctrine-orm-trait 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 77.02k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 setono/doctrine-orm-trait 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 setono/doctrine-orm-trait 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 77.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 23
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-16