proai/eloquent-versioning
Composer 安装命令:
composer require proai/eloquent-versioning
包简介
An extension for the Eloquent ORM to support versioning.
README 文档
README
This is an extension for the Eloquent ORM to support versioning. You can specify attributes as versioned. If an attribute is specified as versioned the value will be saved in a separate version table on each update. It is possible to use timestamps and soft deletes with this feature.
Installation
Eloquent Versioning is distributed as a composer package. So you first have to add the package to your composer.json file:
"proai/eloquent-versioning": "~1.0"
Then you have to run composer update to install the package.
Example
We assume that we want a simple user model. While the username should be fixed, the email and city should be versionable. Also timestamps and soft deletes should be versioned. The migrations would look like the following:
... Schema::create('users', function(Blueprint $table) { $table->increments('id'); $table->integer('latest_version'); $table->string('username'); $table->timestamp('created_at'); }); Schema::create('users_version', function(Blueprint $table) { $table->integer('ref_id')->primary(); $table->integer('version')->primary(); $table->string('email'); $table->string('city'); $table->timestamp('updated_at'); $table->timestamp('deleted_at'); }); ...
The referring Eloquent model should include the code below:
<?php namespace Acme\Models; use Illuminate\Database\Eloquent\Model; use ProAI\Versioning\Versionable; use ProAI\Versioning\SoftDeletes; class User extends Model { use Versionable, SoftDeletes; public $timestamps = true; public $versioned = ['email', 'city', 'updated_at', 'deleted_at']; ... }
Usage
Database Tables
You need to add the following columns to your main model table:
latest_version(integer).
Furthermore you need a version table. The name of the version table is identical with the name of the main model table (e. g. for a model table users the name would be users_version). This table must contain the following columns:
ref_followed by the name of the model's primary key (if the primary key isid, the column name will beref_id)version(integer)
Eloquent Models
You have to define a $versioned array in your model that contains all versioned columns.
Database Queries
Query the database
By default the query builder will fetch the latest version (e. g. User::find(1); will return the latest version of user #1). If you want a specific version or all versions, you can use the following:
-
version(VERSION_NO)returns a specific version
Example:User::version(2)->find(1)will return version #2 of user #1 -
allVersions()returns all versions of the queried items
Example:User::allVersions()->get()will return all versions of all users -
moment(Carbon)returns a specific version, closest but lower than the input date
Example:User::moment(Carbon::now()->subWeek()->find(1)will return the version at that point in time.
Create, update and delete records
All these operations can be performed normally. The package will automatically generate a version 1 on create, the next version on update and will remove all versions on delete.
Timestamps
You can use timestamps in two ways. For both you have to set $timestamps = true;.
-
Normal timestamps
The main table must include acreated_atand aupdated_atcolumn. Theupdated_atcolumn will be overriden on every update. So this is the normal use of Eloquent timestamps. -
Versioned timestamps
If you addupdated_atto your$versionedarray, you need acreated_atcolumn in the main table and aupdated_atcolumn in the version table (see example). On update theupdated_atvalue of the new version will be set to the current time. Theupdated_atvalues of previous versions will not be updated. This way you can track the dates of all updates.
Soft Deletes
If you use the Versionable trait with soft deletes, you have to use the ProAI\Versioning\SoftDeletes trait from this package instead of the Eloquent soft deletes trait.
-
Normal soft deletes
Just use adeleted_atcolumn in the main table. Then on delete or on restore thedeleted_atvalue will be updated. -
Versioned soft deletes
If you create adeleted_atcolumn in the version table and adddeleted_atto the$versionedarray, then on delete or on restore thedeleted_atvalue of the new version will get updated (see example). Thedeleted_atvalues of previous versions will not be updated. This way you can track all soft deletes and restores.
Custom Query Builder
If you want to use a custom versioning query builder, you will have to build your own versioning trait, but that's pretty easy:
<?php namespace Acme\Versioning; trait Versionable { use \ProAI\Versioning\BaseVersionable; public function newEloquentBuilder($query) { return new MyVersioningBuilder($query); } }
Obviously you have to replace MyVersioningBuilder by the classname of your custom builder. In addition you have to make sure that your custom builder implements the functionality of the versioning query builder. There are some strategies to do this:
- Extend the versioning query builder
ProAI\Versioning\Builder - Use the versioning builder trait
ProAI\Versioning\BuilderTrait - Copy and paste the code from the versioning query builder to your custom builder
Support
Bugs and feature requests are tracked on GitHub.
License
This package is released under the MIT License.
proai/eloquent-versioning 适用场景与选型建议
proai/eloquent-versioning 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.94k 次下载、GitHub Stars 达 71, 最近一次更新时间为 2015 年 06 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「orm」 「version」 「Audit」 「versioning」 「laravel」 「audits」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 proai/eloquent-versioning 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 proai/eloquent-versioning 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 proai/eloquent-versioning 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Log adding/updating/deleting of elements
Doctrine ORM provider for the auditor audit-log library.
Kinikit - PHP Application development framework MVC component
Versioning behaviour for eloquent models
PHP Database ORM for Symfony1. Do NOT use for new projects: please move to a newest Symfony release and Doctrine2
Converts SemVer version (like Composer packages) into integer version with operators. Helps managing versions: store, compare, sort and retrive by conditions.
统计信息
- 总下载量: 25.94k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 71
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-19