develoopin/versionable
Composer 安装命令:
composer require develoopin/versionable
包简介
Allows to create Laravel 4 / 5 / 6 /7 / 8 Model versioning and restoring
README 文档
README
Easy to use Model versioning for Laravel
Keep track of all your model changes and revert to previous versions of it.
// Restore to the previous change $content->previousVersion()->revert(); // Get model from a version $oldModel = Version::find(100)->getModel();
Installation
In order to add Versionable to your project, just add
"develoopin/versionable": "~3.0"
to your composer.json. Then run composer install or composer update.
Or run composer require develoopin/versionable if you prefere that.
Run the migrations to create the "versions" table that will hold all version information.
php artisan migrate --path=vendor/develoopin/versionable/src/migrations
Usage
Let the Models you want to set under version control use the VersionableTrait.
class Content extends Model { use develoopin\Versionable\VersionableTrait; }
That's it!
Every time you update your model, a new version containing the previous attributes will be stored in your database.
All timestamps and the optional soft-delete timestamp will be ignored.
Exclude attributes from versioning
Sometimes you don't want to create a version every time an attribute on your model changes. For example your User model might have a last_login_at attribute.
I'm pretty sure you don't want to create a new version of your User model every time that user logs in.
To exclude specific attributes from versioning, add a new array property to your model named dontVersionFields.
class User extends Model { use develoopin\Versionable\VersionableTrait; /** * @var array */ protected $dontVersionFields = [ 'last_login_at' ]; }
Maximum number of stored versions
You can control the maximum number of stored versions per model. By default, there will be no limit and all versions will be saved. Depending on your application, this could lead to a lot of versions, so you might want to limit the amount of stored versions.
You can do this by setting a $keepOldVersions property on your versionable models:
class User { use VersionableTrait; // Keep the last 10 versions. protected $keepOldVersions = 10; }
Retrieving all versions associated to a model
To retrieve all stored versions use the versions attribute on your model.
This attribute can also be accessed like any other Laravel relation, since it is a MorphMany relation.
$model->versions;
Getting a diff of two versions
If you want to know, what exactly has changed between two versions, use the version model's diff method.
The diff method takes a version model as an argument. This defines the version to diff against. If no version is provided, it will use the current version.
/** * Create a diff against the current version */ $diff = $page->previousVersion()->diff(); /** * Create a diff against a specific version */ $diff = $page->currentVersion()->diff( $version );
The result will be an associative array containing the attribute name as the key, and the different attribute value.
Revert to a previous version
Saving versions is pretty cool, but the real benefit will be the ability to revert to a specific version.
There are multiple ways to do this.
Revert to the previous version
You can easily revert to the version prior to the currently active version using:
$content->previousVersion()->revert();
Revert to a specific version ID
You can also revert to a specific version ID of a model using:
$revertedModel = Version::find( $version_id )->revert();
Disable versioning
In some situations you might want to disable versioning a specific model completely for the current request.
You can do this by using the disableVersioning and enableVersioning methods on the versionable model.
$user = User::find(1); $user->disableVersioning(); // This will not create a new version entry. $user->update([ 'some_attribute' => 'changed value' ]);
Use different version table
Some times we want to have models versions in differents tables. By default versions are stored in the table 'versions', defined in develoopin\Versionable\Version::$table.
To use a different table to store version for some model we have to change the table name. To do so, create a model that extends develoopin\Versionable\Version and set the $table property to another table name.
class MyModelVersion extends Version { $table = 'mymodel_versions'; ... }
In the model that you want it use this specific versions table, use the VersionableTrait Trait and add the property $versionClass with value the specific version model.
class MyModel extends Eloquent { use VersionableTrait ; protected $versionClass = MyModelVersion::class ; ... }
And do not forget to create a migration for this versions table, exactly as the default versions table.
License
Versionable is free software distributed under the terms of the MIT license.
develoopin/versionable 适用场景与选型建议
develoopin/versionable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 253 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「version」 「model」 「laravel」 「ardent」 「history」 「restore」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 develoopin/versionable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 develoopin/versionable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 develoopin/versionable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Versioning behaviour for eloquent models
Converts SemVer version (like Composer packages) into integer version with operators. Helps managing versions: store, compare, sort and retrive by conditions.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Display a version number
Interfaces for web resource models and services to retrieve and create them
PHP Version Manager for Windows CLI
统计信息
- 总下载量: 253
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-09-25