tourze/doctrine-async-insert-bundle
Composer 安装命令:
composer require tourze/doctrine-async-insert-bundle
包简介
A Symfony bundle that provides asynchronous database insert operations for Doctrine entities, built on top of Symfony Messenger component for high-performance data insertion.
README 文档
README
A Symfony bundle that provides asynchronous database insert operations for Doctrine entities, built on top of Symfony Messenger component for high-performance data insertion.
Features
- 🚀 Asynchronous database insert operations - Execute database inserts asynchronously to improve response times
- 🔍 Duplicate entry detection and handling - Configurable duplicate entry error handling with logging
- ⏰ Delayed execution support - Schedule inserts with configurable delays using Symfony Messenger DelayStamp
- 📝 Comprehensive logging - Full logging for all insert operations with detailed error tracking
- 🔧 Fallback mechanism - Automatic fallback to synchronous insert when async fails
- 🎯 Entity-aware - Direct entity support with automatic SQL generation
- 🔄 Environment-based control - Force synchronous mode via environment variable
Installation
composer require tourze/doctrine-async-insert-bundle
Quick Start
Bundle Registration
Add to your config/bundles.php:
return [ // ... other bundles Tourze\DoctrineAsyncInsertBundle\DoctrineAsyncInsertBundle::class => ['all' => true], ];
Basic Usage
<?php use Tourze\DoctrineAsyncInsertBundle\Service\AsyncInsertService; /** @var AsyncInsertService $asyncInsertService */ $asyncInsertService = $container->get(AsyncInsertService::class); // Insert entity asynchronously $entity = new YourEntity(); $entity->setName('example'); $asyncInsertService->asyncInsert($entity);
Documentation
Configuration
Environment Variables
FORCE_REPOSITORY_SYNC_INSERT=true- Force synchronous insert mode (bypasses async)
Messenger Configuration
Ensure your config/packages/messenger.yaml includes the routing:
framework: messenger: routing: Tourze\DoctrineAsyncInsertBundle\Message\InsertTableMessage: async
Advanced Usage
Delay and Duplicate Handling
<?php // Insert with 5-second delay $asyncInsertService->asyncInsert($entity, 5000); // Allow duplicate entries $asyncInsertService->asyncInsert($entity, 0, true);
Manual Message Dispatch
<?php use Tourze\DoctrineAsyncInsertBundle\Message\InsertTableMessage; // Create an insert message manually $message = new InsertTableMessage(); $message->setTableName('your_table'); $message->setParams([ 'column1' => 'value1', 'column2' => ['array', 'will', 'be', 'json_encoded'], ]); $message->setAllowDuplicate(false); // Dispatch the message using Symfony Messenger $messageBus->dispatch($message);
Architecture
Components
- AsyncInsertService - Main service for asynchronous entity insertion
- InsertTableMessage - Message object for async processing
- InsertTableHandler - Message handler for processing insert operations
- DoctrineAsyncInsertBundle - Bundle class for service registration
Flow
- Entity is passed to
AsyncInsertService - SQL and parameters are extracted from entity
InsertTableMessageis created and dispatchedInsertTableHandlerprocesses the message asynchronously- On failure, fallback to synchronous insert
Error Handling
The bundle includes comprehensive error handling:
- Duplicate entry errors - Configurable handling with logging
- Message dispatch failures - Automatic fallback to direct insert
- Insert failures - Detailed error logging with context
Dependencies
This bundle depends on:
symfony/messenger- For asynchronous message handlingdoctrine/orm- For entity managementdoctrine/dbal- For database operationstourze/doctrine-direct-insert-bundle- For fallback direct insertiontourze/doctrine-entity-checker-bundle- For SQL formatting
Testing
Run the test suite:
./vendor/bin/phpunit packages/doctrine-async-insert-bundle/tests
Coverage: All tests pass with 100% assertion coverage.
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.
tourze/doctrine-async-insert-bundle 适用场景与选型建议
tourze/doctrine-async-insert-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.7k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 06 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tourze/doctrine-async-insert-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tourze/doctrine-async-insert-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 9.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 19
- 依赖项目数: 32
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-11