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.
Installs: 4 778
Dependents: 27
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/tourze/doctrine-async-insert-bundle
Requires
- php: ^8.1
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/messenger: ^6.4
- symfony/service-contracts: ^3.5
- symfony/yaml: ^6.4 || ^7.1
- tourze/async-contracts: 0.0.*
- tourze/bundle-dependency: 0.0.*
- tourze/doctrine-direct-insert-bundle: 0.0.*
- tourze/doctrine-entity-checker-bundle: ~0.0.8
- yiisoft/json: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
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 dispatched
- InsertTableHandlerprocesses 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 handling
- doctrine/orm- For entity management
- doctrine/dbal- For database operations
- tourze/doctrine-direct-insert-bundle- For fallback direct insertion
- tourze/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.