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.
anfischer/cloner 适用场景与选型建议
anfischer/cloner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 208 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 05 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cloner」 「anfischer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anfischer/cloner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anfischer/cloner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anfischer/cloner 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easily clone sections, entry types, groups and more.
This is a Symfony Bundle helps you to copy an entity
Laravel Foundation - a framework inspired by Lucid Foundation
Contao backend module to clone dummy pages, content elements, modules and rewire references.
A PHP implementation of the Data Transfer Object pattern (https://martinfowler.com/eaaCatalog/dataTransferObject.html)
A package containing base classes to be used as a starting ground for implementing the Specification pattern in PHP.
统计信息
- 总下载量: 208
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-05-25