pavelvais/upsert-doctrine

Doctrine helper for update and insert functionality

v0.2.1 2023-10-22 17:35 UTC

This package is auto-updated.

Last update: 2024-05-22 19:18:30 UTC


README

PHP Unit Tests

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.

Installation

Install Upsert-Doctrine using Composer:

composer require pavelvais/upsert-doctrine

Usage

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
}

Local Testing with Docker

This project includes a Docker setup to run local testing and development. Here are the steps to get started:

  1. 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
  2. Installing Dependencies: Install the project dependencies using Composer with the following command:

    make composer-install
  3. Running Tests: Run the PHPUnit tests to ensure everything is working as expected.

    make test

Roadmap

  • Basic Upsert Functionality
  • Batch Upsert method
  • Query Manager
  • Postgres Support
  • Live database testing

Contributing

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.

License

This project is licensed under the MIT License - see the LICENSE file for details.