nexuspoint/laravel-versioned

此包已被弃用且不再维护。未建议替代包。

Eloquent 模型版本控制

1.0.2 2016-01-04 15:06 UTC

This package is not auto-updated.

Last update: 2023-02-04 09:14:00 UTC


README

一个用于处理任何 Eloquent 模型版本控制的 Laravel 5 包。

示例用法

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

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

帖子已保存。没有之前的版本。

现在我们可以更新帖子。

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

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

我们已经自动进行了版本控制。

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

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

再次。

$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,
]

现在我们已经将数据恢复到版本 1,但作为一个新版本。

$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,
]

回滚实际上是对模型的“撤销”操作。它还会删除历史记录,因此我们回到了版本 3。

安装

使用 Composer 安装

composer require nexuspoint/versioned

然后创建以下迁移

<?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');
    }
}

运行迁移

php artisan migrate

将特质添加到您的 Eloquent 类中,并将 $versionNameColumn 设置为您想记录为版本名称字段的字段。

<?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';
}

其他方法

手动添加模型的当前状态的新版本。我建议在此之后保存模型。通常您不需要手动调用此操作,因为模型在每次更改时都会自动进行版本控制。

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

获取所有版本以在 UI 中选择一个版本

$post->getAllVersions();

获取特定版本。此方法将返回正确的模型,以便可以使用任何获取修饰符或方法,并且您可以在 UI 中显示上一个版本。

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

获取当前版本号。

$post->getCurrentVersionNumber();

获取用于在 UI 中显示的上一个版本。

$post->getPreviousVersion();

恢复到上一个版本。给定版本号之后的所有版本将被保留,当前版本也将保存,以便您可以稍后撤销。

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

类似于恢复版本,这会删除给定版本号之后的所有历史记录,所以实际上就像回到过去一样。

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

回滚到最后一个版本。这实际上是一个“撤销”功能,用于删除最新版本。

$post->rollback();

删除指定的版本。

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

删除模型的所有版本历史。

$post->deleteAllVersions();

即将推出的功能

我想将一个 JavaScript 差分引擎集成到这个包中,以便用户可以立即看到版本之间的变化。

我很快将添加一些测试。

贡献

欢迎提交修复错误和添加新功能的拉取请求。