anfischer/cloner
Composer 安装命令:
composer require anfischer/cloner
包简介
A package which allows for easy recursive cloning and persistence of Laravel Eloquent models
关键字:
README 文档
README
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
| Laravel | Cloner | PHP |
|---|---|---|
| 5.4.x | 0.1.0 | ^5.4 |
| 6.x | 0.2.0 | ^7.3 |
| 7.x | 0.2.0 | ^7.3 |
| 8.x | 0.2.0 | ^7.3 |
| 9.x | 0.4.0 | ^8.0.2 |
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.
统计信息
- 总下载量: 208
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-05-25