Upsert-Doctrine is a PHP library aimed at providing an elegant solution for upsert operations in Doctrine. This library simplifies the process of either inserting a new record or updating an existing one in a single operation, all within the Doctrine ORM ecosystem.
Install Upsert-Doctrine using Composer:
composer require pavelvais/upsert-doctrine
Below is a basic example demonstrating how to use Upsert-Doctrine:
use PavelVais\UpsertDoctrine\UpsertManager;
$entityManager = // ... get your Doctrine entity manager here
$upsertManager = new UpsertManager($entityManager);
// Example for Single Upsert
$data = [
'book_id' => 1,
'author_id' => 2,
'ownership_type' => 2,
];
$repositoryClass = DoctrineOrmEntity::class; // Replace with your actual repository class
try {
$result = $upsertManager->execute($data, $repositoryClass);
// $result will contain the number of affected rows
} catch (\Exception $e) {
// Handle exceptions
}
// Example for Batch Upsert
$batchData = [
[
'book_id' => 1,
'author_id' => 2,
'ownership_type' => 2,
],
[
'book_id' => 2,
'author_id' => 3,
'ownership_type' => 1,
],
];
try {
$result = $upsertManager->executeBatch($batchData, $repositoryClass);
// $result will contain the number of affected rows
} catch (\Exception $e) {
// Handle exceptions
}
This project includes a Docker setup to run local testing and development. Here are the steps to get started:
-
Build and manage the Containers: Build and start the containers in the background.
make build
To stop and remove the containers, use the following command:
make stop
-
Installing Dependencies: Install the project dependencies using Composer with the following command:
make composer-install
-
Running Tests: Run the PHPUnit tests to ensure everything is working as expected.
make test
- Basic Upsert Functionality
- Batch Upsert method
- Query Manager
- Postgres Support
- Live database testing
Contributions are welcome! For major changes, please open an issue first to discuss what you would like to change. Ensure to update tests as necessary.
This project is licensed under the MIT License - see the LICENSE file for details.