tourze / doctrine-upsert-bundle
Doctrine的upsert支持
0.1.0
2025-04-25 18:47 UTC
Requires
- php: ^8.1
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^2.20 || ^3.0
- doctrine/persistence: ^3.1 || ^4
- nesbot/carbon: ^2.72 || ^3
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/property-access: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/doctrine-entity-checker-bundle: ~0.0.8
- yiisoft/strings: ^2.1
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-05-12 06:08:23 UTC
README
Introduction
doctrine-upsert-bundle provides efficient UPSERT (insert or update) capabilities for Doctrine ORM, supporting major databases like MySQL and SQLite. It automatically generates UPSERT SQL to achieve high-performance batch writing and data synchronization under unique constraints.
Features
- Support for single and batch UPSERT operations
- MySQL: INSERT ... ON DUPLICATE KEY UPDATE
- SQLite: INSERT ... ON CONFLICT ... DO UPDATE SET
- Automatic database platform detection, compatible with MySQL and SQLite
- Automatic generation of UPSERT statements based on entity unique constraints
- Support for custom UPSERT Providers for extensibility
- Friendly error handling, avoiding EntityManager closure
Installation
Requirements
- PHP >= 8.1
- doctrine/orm >= 2.20
- doctrine/dbal >= 3.7
- symfony >= 6.4
Composer Installation
composer require tourze/doctrine-upsert-bundle
Quick Start
Configuration
Make sure to register in config/bundles.php
:
return [ Tourze\DoctrineUpsertBundle\DoctrineUpsertBundle::class => ['all' => true], ];
Basic Usage
use App\Entity\YourEntity; use Tourze\DoctrineUpsertBundle\Service\UpsertManager; // Inject UpsertManager $entity = new YourEntity(); // Set properties ... $upserted = $upsertManager->upsert($entity);
Batch UPSERT
$data = [ ['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar'], ]; $upsertManager->executeBatch($data, YourEntity::class);
Configuration Options
- Support for extending different database platform UPSERTs by implementing ProviderInterface
- Supported database platforms:
- MySQL (MySQLUpsertProvider)
- SQLite (SQLiteUpsertProvider)
- Custom unique constraint field handling logic
Contribution Guidelines
- Describe issues and changes in detail before submitting Issues or PRs
- Follow PSR-12 coding standards
- Ensure new features have test cases
Copyright and License
MIT License © tourze
Update Log
See [CHANGELOG.md] or Git history.