umanit/doctrine-singleton-bundle
Composer 安装命令:
composer require umanit/doctrine-singleton-bundle
包简介
Singleton entities working with Doctrine and Sonata
关键字:
README 文档
README
This bundle intends to easily create complex singleton entities (totally unique, or based on some properties). Sonata Admin friendly with automatic integration.
Install
Register the bundle to your 'app/AppKernel.php'
new Umanit\DoctrineSingletonBundle\UmanitDoctrineSingletonBundle(),
That's it!
Usage
Tell that your entity is a singleton
Just need to implement the Umanit\DoctrineSingletonBundle\Model\SingletonInterface
<?php namespace App\Entity\Content; use Doctrine\ORM\Mapping as ORM; use Umanit\DoctrineSingletonBundle\Model\SingletonInterface; #[ORM\Table(name="page")] class Page implements SingletonInterface { }
Now, if you try to create 2 entities of type "Page", you'll get a NonUniqueException
More complex unicity
If you want to implement a more complex unicity, for example, you want the entity to be unique per local, you can subscribe to the
FilterSingletonEvent::SINGLETON_FILTER_EVENT event to modify the filters (filters are used in a findBy() clause)
e.g (from TranslationBundle)
<?php namespace Umanit\TranslationBundle\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Umanit\DoctrineSingletonBundle\Event\FilterSingletonEvent; use Umanit\TranslationBundle\Doctrine\Model\TranslatableInterface; class SingletonSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return [FilterSingletonEvent::SINGLETON_FILTER_EVENT => ['onFilterEvent']]; } public function onFilterEvent(FilterSingletonEvent $event) { $entity = $event->getEntity(); $filters = $event->getFilters(); if ($entity instanceof TranslatableInterface) { $filters['locale'] = $entity->getLocale(); } $event->setFilters($filters); } }
services.yml
services: umanit_translation.event_subscriber.doctrine_singleton_filter: class: Umanit\TranslationBundle\EventSubscriber\SingletonSubscriber tags: - { name: kernel.event_subscriber }
Get singleton
In order to get your singleton instances, you can use the provided helpers for your PHP code or twig :
<?php $this->get('umanit_doctrine_singleton.helper')->getSingleton('App\Entity\Page::class');
{% set singleton = get_singleton('App\\Entity\\Page') %}
The method getSingleton($className, array $filters = [], $instantiateIfNotFound = false) can take from 1 to 3 arguments :
$className: FQCN of the class to get$filters: Filters to apply to get the singleton (e.g. :getSingleton("MyClass", ["locale" => "en"]))$instantiateIfNotFound: If the entity is not found, returns an empty entity instead ofnull
Integrating into SonataAdmin
The bundle will automatically remove add and list button from the list and the edit view.
If you use the UmanitDoctrineSingletonBundle:Sonata\SingletonCRUD as a controller for your admin class, you'll have a redirection
to create if there's no entity found, to edit if there's only one, and to list if you have more than one.
e.g :
app.admin.page: class: App\Admin\PageAdmin arguments: [~, App\Entity\Content\Page, UmanitDoctrineSingletonBundle:Sonata\SingletonCRUD ] tags: - { name: sonata.admin, manager_type: orm, group: 'Content', label: 'Page' }
umanit/doctrine-singleton-bundle 适用场景与选型建议
umanit/doctrine-singleton-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.66k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2018 年 06 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「doctrine」 「singleton」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 umanit/doctrine-singleton-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 umanit/doctrine-singleton-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 umanit/doctrine-singleton-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
Laravel container extension for resolving and defining singletons alongside classes
Make pimcore migration simple
Bundle Symfony DaplosBundle
PHP5 singleton trait
redis单例多库,支持ThinkPHP5,ThinkPHP6,Laravel6
统计信息
- 总下载量: 7.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-06-21