vdlp/eloquent-model-cloner
Composer 安装命令:
composer require vdlp/eloquent-model-cloner
包简介
Clone eloquent models and relations
README 文档
README
This package was inspired by https://github.com/BKWLD/cloner and modified to also work with OctoberCMS installations.
A trait for Laravel Eloquent models that lets you clone a model and it's relationships, including files. Even to another database.
Installation
To get started with the Eloquent Model Cloner, use Composer to add the package to your project's dependencies:
composer require vdlp/eloquent-model-cloner
Usage
Your model should now look like this:
class Article extends Eloquent { use \Vdlp\EloquentModelCloner\Cloneable; }
You can clone an Article model like so:
$clone = Article::first()->duplicate();
Cloning Relationships
Lets say your Article has many Photos (a one to many relationship) and can have more than one Authors (a many to many relationship). Now, your Article model should look like this:
class Article extends Eloquent { use \Vdlp\EloquentModelCloner\Cloneable; protected $cloneableRelations = ['photos', 'authors']; public function photos() { return $this->hasMany('Photo'); } public function authors() { return $this->belongsToMany('Author'); } }
The $cloneableRelations informs the Cloneable as to which relations it should follow when cloning.
Now when you call Article::first()->duplicate(), all of the Photo rows of the original will be copied and associated with the new Article.
And new pivot rows will be created associating the new Article with the Authors of the original (because it is a many to many relationship, no new Author rows are created).
Furthermore, if the Photo model has many of some other model, you can specify $cloneableRelations in its class and Cloner will continue replicating them as well.
Customizing the cloned attributes
By default, Cloner does not copy the id (or whatever you've defined as the key for the model) field; it assumes a new value will be auto-incremented.
It also does not copy the created_at or updated_at.
You can add additional attributes to ignore as follows:
class Photo extends Eloquent { use \Vdlp\EloquentModelCloner\Cloneable; protected $cloneExemptAttributes = ['uid', 'source']; public function article() { return $this->belongsTo('Article'); } public function onCloning($src, $child = null) { $this->uid = str_random(); if ($child) { echo 'This was cloned as a relation!'; } echo 'The original key is: ' . $src->getKey(); } }
The $cloneExemptAttributes adds to the defaults.
If you want to replace the defaults altogether, override the trait's getCloneExemptAttributes() method and return an array.
Also, note the onCloning() method in the example.
It is being used to make sure a unique column stays unique.
The Cloneable trait adds to no-op callbacks that get called immediately before a model is saved during a duplication and immediately after: onCloning() and onCloned().
The $child parameter allows you to customize the behavior based on if it's being cloned as a relation or direct.
In addition, Cloner fires events during cloning and when the model has been cloned, see:
\Vdlp\EloquentModelCloner\Events\Cloned\Vdlp\EloquentModelCloner\Events\Cloning
The event payload contains the clone and the original model instances.
vdlp/eloquent-model-cloner 适用场景与选型建议
vdlp/eloquent-model-cloner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.5k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 07 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「vdlp」 「eloquent-model-cloner」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vdlp/eloquent-model-cloner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vdlp/eloquent-model-cloner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 16.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-07-29