mrynarzewski/dictionary-bundle
Composer 安装命令:
composer require mrynarzewski/dictionary-bundle
包简介
Symfony bundle for managing reusable dictionaries and dictionary items.
README 文档
README
Reusable Symfony bundle for storing business dictionaries and dictionary items in Doctrine.
What was recently changed
- minimum PHP requirement raised to
^8.2 - dependency constraints tightened for Symfony
6.4/7.xand Doctrine - Doctrine mapping migrated to PHP attributes
- custom validator fixed to validate dictionary item values correctly
- Docker-based development environment added
Package scope
The bundle provides:
DictionaryandDictionaryItemDoctrine entities- repositories for querying dictionary items
- services for creating and updating dictionaries and items
BelongToDictionaryvalidator for checking whether a scalar value exists in a given dictionaryDictionaryItemTypeform type
Deletion is intentionally blocked by the services, so the bundle treats dictionaries as reference data.
Installation in a Symfony project
- Install the bundle with Composer. Choose one of the following options:
Standard Composer installation:
composer require mrynarzewski/dictionary-bundle
Local development with a Composer path repository:
{
"repositories": [
{
"type": "path",
"url": "../symfony-dictionary-bundle"
}
]
}
Then require the package:
composer require mrynarzewski/dictionary-bundle:*
- Register the bundle in
config/bundles.phpif Symfony Flex does not do it automatically:
<?php
return [
Mrynarzewski\DictionaryBundle\MrynarzewskiDictionaryBundle::class => ['all' => true],
];
- Add Doctrine mapping for the bundle entities in
config/packages/doctrine.yaml:
doctrine:
orm:
mappings:
MrynarzewskiDictionaryBundle:
is_bundle: false
type: attribute
dir: '%kernel.project_dir%/vendor/mrynarzewski/dictionary-bundle/Entity'
prefix: 'Mrynarzewski\DictionaryBundle\Entity'
alias: MrynarzewskiDictionaryBundle
- If your application does not use Doctrine migrations yet, install the Symfony integration:
composer require doctrine/doctrine-migrations-bundle
- Run the migrations. The bundle ships its own migration and registers its migrations path automatically when
doctrine/doctrine-migrations-bundleis installed:
php bin/console doctrine:migrations:migrate
Using the bundle
The bundle exposes two Doctrine entities:
Mrynarzewski\DictionaryBundle\Entity\DictionaryMrynarzewski\DictionaryBundle\Entity\DictionaryItem
It also exposes two main services through their interfaces:
Mrynarzewski\DictionaryBundle\Service\DictionaryServiceInterfaceMrynarzewski\DictionaryBundle\Service\DictionaryItemServiceInterface
Creating dictionaries and items
<?php
namespace App\Service;
use Mrynarzewski\DictionaryBundle\Repository\DictionaryRepository;
use Mrynarzewski\DictionaryBundle\Service\DictionaryItemServiceInterface;
use Mrynarzewski\DictionaryBundle\Service\DictionaryServiceInterface;
final class SeedDictionaryService
{
public function __construct(
private readonly DictionaryServiceInterface $dictionaryService,
private readonly DictionaryItemServiceInterface $dictionaryItemService,
private readonly DictionaryRepository $dictionaryRepository,
) {
}
public function seed(): void
{
$dictionary = $this->dictionaryRepository->find('country');
if ($dictionary === null) {
$dictionary = $this->dictionaryService->create('country', 'Countries');
}
$this->dictionaryItemService->create($dictionary, 'PL', 'Poland');
$this->dictionaryItemService->create($dictionary, 'DE', 'Germany');
}
}
Querying items
Use DictionaryRepository to fetch a dictionary and DictionaryItemRepository to fetch or validate items:
$dictionary = $dictionaryRepository->find('country');
$items = $dictionaryItemRepository->getItemsFromDictionary($dictionary);
$exists = $dictionaryItemRepository->existsInDictionaryByItem($dictionary, 'PL');
Validating a field against a dictionary
The bundle includes the BelongToDictionary constraint, which lets you validate a scalar value against an existing dictionary:
<?php
namespace App\Dto;
use Mrynarzewski\DictionaryBundle\Constraints\BelongToDictionary;
final class AddressDto
{
#[BelongToDictionary(dictionary: 'country')]
public ?string $countryCode = null;
}
Form type
The bundle includes a simple DictionaryItemType form type for DictionaryItem entities:
$form = $this->createForm(\Mrynarzewski\DictionaryBundle\Form\DictionaryItemType::class, $dictionaryItem);
Notes
Dictionary::idis a string identifier, for examplecountryorcurrency.- The bundle stores data in the
dictionaryanddictionary_itemtables. - Each
DictionaryItemis unique within a dictionary by the pair(dictionary_id, item). - The services intentionally do not support deletion, so dictionaries should be treated as reference data.
Local development with Docker
Build and start the PHP container:
docker compose up -d --build
Install dependencies inside the container:
docker compose exec app composer install
Run a quick syntax check:
docker compose exec app find . -path ./vendor -prune -o -name '*.php' -print0 | xargs -0 -n1 php -l
Open a shell in the container:
docker compose exec app bash
Notes on compatibility
The bundle now targets modern Symfony applications while keeping a practical compatibility window:
- PHP
^8.2 - Symfony
^6.4 || ^7.0 - Doctrine ORM
^2.17 || ^3.0
The provided Docker image runs PHP 8.4, so you can validate the bundle on a newer runtime even though the library itself supports PHP 8.2+.
mrynarzewski/dictionary-bundle 适用场景与选型建议
mrynarzewski/dictionary-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 46 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「doctrine」 「dictionary」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mrynarzewski/dictionary-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mrynarzewski/dictionary-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mrynarzewski/dictionary-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
Make pimcore migration simple
Bundle Symfony DaplosBundle
统计信息
- 总下载量: 46
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-05-28