sofa/revisionable

处理数据库版本的好方法。

v5.7.2 2023-01-31 11:08 UTC

README

Code quality Latest Stable Version Downloads

处理数据库版本的好方法。

  • 批量处理版本 - 一个条目覆盖所有创建/更新的字段,这使得比较两个版本或获取单一操作期间更改的所有数据变得非常简单。

需求

  • 此包需要 PHP 5.4+
  • 目前它与 Laravel5 +通用 Illuminate Guard、tymon/jwt-auth 或 cartalyst/sentry 2/sentinel 2 兼容

用法(Laravel5基本示例 - 以下还有定制说明)

1. 下载包或在您的 composer.json 中要求

composer require sofa/revisionable

2. 将服务提供者添加到您的 app/config/app.php

    'providers' => array(

        ...

        'Sofa\Revisionable\Laravel\ServiceProvider',
    ),

3. 发布包配置文件

~$ php artisan vendor:publish [--provider="Sofa\Revisionable\Laravel\ServiceProvider"]

这将在 config/sofa_revisionable.php 文件中创建文件,您可以在其中调整一些设置

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | User model (for executor relation on Revision model).
    |--------------------------------------------------------------------------
    |
    | By default App\User.
    */
    'usermodel' => 'App\User',

    /*
    |--------------------------------------------------------------------------
    | User provider (auth) implementation.
    |--------------------------------------------------------------------------
    |
    | By default Laravel generic Illuminate\Auth\Guard.
    |
    | Supported options:
    |  - illuminate
    |  - sentry
    |  - sentinel
    |  - jwt-auth
    |  - session 
    */
    'userprovider' => 'illuminate',


    /*
    |--------------------------------------------------------------------------
    | User field to be saved as the author of tracked action.
    |--------------------------------------------------------------------------
    |
    | By default:
    |
    |  - id for illuminate
    |  - login field (email) for sentry/sentinel
    */
    'userfield' => 'id',


    /*
    |--------------------------------------------------------------------------
    | Table used for the revisions.
    |--------------------------------------------------------------------------
    */
    'table' => 'revisions',


    /*
    |--------------------------------------------------------------------------
    | Database connection used for the revisions.
    |--------------------------------------------------------------------------
    */
    'connection' => null,

];

4. 运行迁移以创建版本表

~$ php artisan revisions:table
~$ php artisan revisions:upgrade-5.3
~$ php artisan migrate [--database=custom_connection]

如果您想使用非默认数据库连接运行迁移,可以提供额外的 --database 参数。

5. 将可修订属性添加到您希望跟踪的模型

<?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Sofa\Revisionable\Laravel\Revisionable; // trait

class User extends Model
{
    use Revisionable;

    /*
     * Set revisionable whitelist - only changes to any
     * of these fields will be tracked during updates.
     */
    protected $revisionable = [
        'email',
        'name',
    ];

这样就可以开始使用它了!

L5中的定制

默认行为:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sofa\Revisionable\Laravel\Revisionable;

class Ticket extends Model
{
    use Revisionable;
}
$ php artisan tinker

>>> $ticket = App\Models\Ticket::first();
=> <App\Models\Ticket>

>>> $revision->getDiff();
=> [
       "customer_id"    => [
           "old" => "1",
           "new" => "101"
       ],
       "item_id"        => [
           "old" => "2",
           "new" => "1"
       ],
       "responsible_id" => [
           "old" => "8",
           "new" => "2"
       ]
   ]

>>> $revision->old('item_id');
=> "2"

>>> $revision->new('item_id');
=> "1"

>>> $revision->isUpdated('item_id');
=> true

>>> $revision->isUpdated('note');
=> false

>>> $revision->label('item_id');
=> "item_id"

>>> $revision->old;
=> [
       "defect"         => "nie dziala",
       "note"           => "wrocilo na gwarancji",
       "customer_id"    => "1",
       "item_id"        => "2",
       "responsible_id" => "8",
       "status_id"      => "6"
   ]

>>> $revision->action;
=> "updated"

但在这里,您可以利用捆绑的 Presenter 来进行有用的调整

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Sofa\Revisionable\Laravel\Revisionable;

class Ticket extends Model
{
    use Revisionable;

    protected $revisionPresenter = 'App\Presenters\Revisions\Ticket';
}
namespace App\Presenters\Revisions;

use Sofa\Revisionable\Laravel\Presenter;

class Ticket extends Presenter
{
    protected $labels = [
        'item_id'        => 'Przedmiot',
        'customer_id'    => 'Klient',
        'status_id'      => 'Status',
        'responsible_id' => 'Serwisant',
        'defect'         => 'Usterka',
        'note'           => 'Uwagi',
    ];

    protected $passThrough = [
        'item_id'        => 'item.name',
        'customer_id'    => 'customer.name',
        'responsible_id' => 'serviceman.name',
        'status_id'      => 'status.name',
    ];

    protected $actions = [
        'created'  => 'utworzony',
        'updated'  => 'edytowany',
        'deleted'  => 'usunięty',
        'restored' => 'przywrócony',
    ];

}

然后

$ php artisan tinker

>>> $ticket = App\Models\Ticket::first();
=> <App\Models\Ticket>

>>> $revision->old('item_id'); // value fetched from the relationship
=> "komputer pc"

>>> $revision->new('item_id'); // value fetched from the relationship
=> "laptop acer"

>>> $revision->label('item_id'); // custom label defined in the presenter
=> "Przedmiot"

>>> $revision->action; // custom action name defined in the presenter
=> "edytowany"