定制 nexuspoint/laravel-versioned 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nexuspoint/laravel-versioned

最新稳定版本:1.0.2

Composer 安装命令:

composer require nexuspoint/laravel-versioned

包简介

Versioning for Eloquent Models

README 文档

README

A Laravel 5 package to handle versioning on any of your Eloquent models.

Example Usage

$post = new App\Post();
$post->title = 'Some title';
$post->body = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.';
$post->save();

echo $post->getCurrentVersion(); // 1

The post is saved. There are no previous versions.

Now we can update the post.

$post->body = 'New body text';
$post->save();

echo $post->getCurrentVersion(); // 2

We've automatically versioned it.

$post->body = 'Even newer body text';
$post->save();

echo $post->getCurrentVersion(); // 3

And again.

$post->restoreVersion(1); // true

echo $post->getCurrentVersion(); // 4

$post->toArray();
    
[
     "title" => "Some title",
     "body" => "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
     "updated_at" => "2015-09-15 19:43:45",
     "created_at" => "2015-09-15 19:41:17",
     "id" => 1,
]

Now we've restored the data as it was at version 1, but as a new version.

$post->rollback();

echo $post->getCurrentVersion(); // 3

$post->toArray();

[
     "title" => "Some title",
     "body" => "Even newer body text",
     "updated_at" => "2015-09-15 19:43:45",
     "created_at" => "2015-09-15 19:41:17",
     "id" => 1,
]

Rollback is effectively an 'undo' on the model. It also removes the history so we're back at version 3.

Installation

Install with Composer:

composer require nexuspoint/versioned

Then create the following migration:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateVersionsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('versions', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('version_no')->unsigned();
            $table->integer('subject_id')->unsigned();
            $table->string('subject_class');
            $table->string('name');
            $table->text('data');
            $table->string('hash');
            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('versions');
    }
}

run migrations

php artisan migrate

Add the trait to your Eloquent class, and set $versionNameColumn to the field that you want to record as the version name column.

<?php
use Illuminate\Database\Eloquent\Model;
use NexusPoint\Versioned\Versioned;

class Post extends Model
{
    use Versioned;
    
    /**
     * Field from the model to use as the versions name
     * @var string
     */
    protected $versionNameColumn = 'title';
}

Other methods

Manually add a new version of the model in its current state. I recommend to save the model after this. Normally you wouldn't need to call this manualy as the model will automatically version every time it is changed.

$post->addVersion('optional version name');

Get all versions for display in UI to select a version

$post->getAllVersions();

Get a specific version. This method will return the correct model so that any get mutators or methods are useable and you can display a previous version in your UI.

$post->getVersion({version number});

Get current version number.

$post->getCurrentVersionNumber();

Get previous version for display in UI.

$post->getPreviousVersion();

Restore to a previous version. All versions after the given version number will be conserved, and the current version will also be saved so that you can later undo.

$post->restoreVersion({version number});

Similar to restore version, this deletes all the history after the given version so really is like going back in time.

$post->rollbackToVersion({version number});

Rollback to the last version. This is effectively an 'undo' function to remove the latest version.

$post->rollback();

Delete a given version.

$post->deleteVersion({version number});

Delete all version history of a model.

$post->deleteAllVersions();

Upcoming Features

I'd like to integrate a javascript diff engine to this package so that users can immediately see what has changed between versions.

I'll be adding some tests soon.

Contributing

Pull requests welcome for bug fixes, and new useful features.

nexuspoint/laravel-versioned 适用场景与选型建议

nexuspoint/laravel-versioned 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.51k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2015 年 09 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「versioning」 「laravel」 「versioned」 「undo」 「laravel-5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nexuspoint/laravel-versioned 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nexuspoint/laravel-versioned 我们能提供哪些服务?
定制开发 / 二次开发

基于 nexuspoint/laravel-versioned 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-15