anfischer/cloner

A package which allows for easy recursive cloning and persistence of Laravel Eloquent models

v0.4.1 2022-09-22 13:28 UTC

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

A package which allows for easy recursive cloning and persistence of Laravel Eloquent models, including:

  • Recursive cloning of Eloquent models and their relationships without forced persistence, allowing for in-memory changes to cloned models before they are saved to the database
  • Persistence of recursive relationships including cloned pivot data

Since this is a feature I commonly rely on in client projects, I decided to extract the functionality into a package. However this also has the consequence that your mileage may vary, and hence pull requests are welcome - please see CONTRIBUTING for details.

Structure

src/
tests/
vendor/

Version Compatibility

Install

Via Composer

$ composer require anfischer/cloner

The package will automatically register its service provider.

Usage

Basic Usage

use Anfischer\Cloner;

$clone = (new CloneService)->clone($someEloquentModel);
$persistedModel = (new PersistenceService)->persist($clone);

or

$cloner = new Cloner(new CloneService, new PersistenceService);
$clone = $cloner->clone($someEloquentModel);
$persistedModel = $cloner->persist($clone);

or

$clone = \Cloner::clone($someEloquentModel);
$persistedModel = \Cloner::persist($clone);

Convenience Methods

Cloner also exposes a convinience method for cloning and persisting at the same time:

$cloner = new Cloner(new CloneService, new PersistenceService);
$persistedModel = $cloner->cloneAndPersist($someEloquentModel);

Cloned Model Map

You may wish to keep track of which models were cloned and the keys of their respective clones. In order to do this Cloner keeps a record of these keys.

$cloneService = new CloneService()

// $personModel->id === 1;
// gettype($personModel) === App\Person;

$clone = ($cloneService)->clone($personModel);

$persistedModel = (new PersistenceService)->persist($clone);
// or
$persistedModel = $clone->save();

// $persistedModel->id === 2

$map = $cloneService->getKeyMap();

// $map === [App\Person => [1 => 2]];

Configuration

To publish the config file to config/cloner.php run:

php artisan vendor:publish --provider="Anfischer\Cloner\ClonerServiceProvider"

Cloner supports various persistence strategies by default. These can be configured by modifying the configuration in config/cloner.php.

For example

return [

    'persistence_strategies' => [
        Illuminate\Database\Eloquent\Relations\HasOne::class =>
            Anfischer\Cloner\Strategies\PersistHasOneRelationStrategy::class,
        Illuminate\Database\Eloquent\Relations\HasMany::class =>
            Anfischer\Cloner\Strategies\PersistHasManyRelationStrategy::class,
        Illuminate\Database\Eloquent\Relations\BelongsToMany::class =>
            Anfischer\Cloner\Strategies\PersistBelongsToManyRelationStrategy::class,

        // You can add your own strategies for relations
        SomePackage\Relations\CustomRelation =>
            App\Cloner\PersistenceStrategies\PersistSomePackageCustomRelationStrategy
    ]
];

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Security

If you discover any security related issues, please email kontakt@season.dk instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.