enea/laravel-sequenceable

简化数据库中序列值生成和自动补全

v4.2.0 2024-09-14 21:51 UTC

README

Build Status Software License

Laravel Sequenceable 是一个用于生成和管理 Laravel 模型序列的库。

// simple sequence
Serie::lineal('document_number');

// sequence with scope
Serie::lineal('document_number')->scope('invoice');

// sequence with scope and fixed length
Serie::lineal('document_number')->scope('invoice')->length(8);

安装

Laravel Sequenceable 需要 PHP 8.1。

要获取最新版本,只需使用 Composer 需求项目

composer require enea/laravel-sequenceable

安装后,如果您没有使用自动包发现,那么您需要在您的 config/app.php 中注册 Enea\Sequenceable\SequenceableServiceProvider 服务提供者。

现在,发布配置文件。

php artisan vendor:publish --provider='Enea\Sequenceable\SequenceableServiceProvider'

最后运行迁移。

php artisan migrate

基本用法

使用库非常简单,只需使用 Sequenceable 特性和实现 SequenceableContract 接口,然后您只需指定要生成的序列。

<?php namespace App;

use Enea\Sequenceable\Contracts\SequenceableContract;
use Enea\Sequenceable\Sequenceable;
use Enea\Sequenceable\Serie;
use Illuminate\Database\Eloquent\Model;

class Document extends Model implements SequenceableContract
{
    use Sequenceable;

    public function sequencesSetup(): array
    {
      return [
          Serie::lineal('document_number')
      ];
    }
}

高级

我们以支付文档为例,展示了生成序列的所有选项。

  • 首先,我们需要一个列来存储序列,为此我们将使用名为 number 的列。
    public function sequencesSetup(): array
    {
        return [ Serie::lineal('document_number') ];
    }
  • 现在我们已经定义了列,我们意识到我们需要为每种类型的文档创建单独的序列。为此,库提供了为列添加作用域的可能性。
    public function sequencesSetup(): array
    {
        return [ 
            Serie::lineal('document_number')->scope($this->type())
        ];
    }

  	protected function type(): string
    {
        return $this->payment_document_type;
    }
  • 一切都很好,但现在我们希望序列不是以数值形式保存,而是以固定长度为 10 的文本字符串保存。
    public function sequencesSetup(): array
    {
        return [ 
            Serie::lineal('document_number')->scope($this->type())->length(10)
        ];
    }
  • 最后,我们可以说我们不想使用默认的序列表,我们需要一个特殊的表来存储支付序列,为此您必须创建自己的序列表

    我们可以使用类 Enea\Sequenceable\Wrap::create 来包装一组序列。

    public function sequencesSetup(): array
    {
        return [ 
            Wrap::create(PaymentSequence::class, 
                         fn(Wrap $wrap) => $wrap->column('document_number')->scope($this->type())->length(10))
        ];
    }

列表

要检索模型的所有序列,您可以使用与 Enea\Sequenceable\Succession 类关联的 Enea\Sequenceable\Facades\Succession 门面。

$collection = Succession::from(Document::class);

这返回了一个 Enea\Sequenceable\SequenceCollection 实例。您可以使用它做以下事情

// return all sequences
$collection->all();

// find sequence by name
$collection->find('document_number', 'invoice');

配置

您可以在 config\sequenceable.phpmodel 键中更改默认序列模型。

<?php
return [    
    /*
    |--------------------------------------------------------------------------
    | Sequence Model
    |--------------------------------------------------------------------------
    |
    | This key defines the base sequence model that will be used to generate the autoincrementable 
    | values, you can modify this key and define your own sequence model whenever 
    | you implement the SequenceContract interface or extend the base model
    */
   'model' => \Enea\Sequenceable\Model\Sequence::class,
];

或明确指定要使用的模型以及某些字段,您可以从模型中的序列配置中实现这一点。

    public function sequencesSetup(): array
    {
        return [ 
             Wrap::create(CustomSequence::class, function(Wrap $wrap): void {
                $wrap->column('column-name');                
                $wrap->column('another-column-name');
                //..
            }),
        ];
    }

自定义

如果您已经有了用于存储序列的模型,您需要实现 Enea\Sequenceable\Contracts\SequenceContract 接口,或扩展默认模型 Enea\Sequenceable\Model\Sequence

如果您有自己的序列模型,有一些字段您应该存储在其序列表中

  1. 列 ID,这是通过连接列名和作用域获得的。
  2. 序列所属的 的名称。
  3. 一个整型 序列

示例

为了更好地说明这一点,我们将使用默认的 Sequence 模型。

该模型包含默认配置。

迁移

表结构包含所需的字段,您可以在 CreateSequencesTable 中查看迁移。

您可以在 test 文件夹中找到这个示例的另一个例子,并查找文件 CustomSequence.phpmigrations/2017_04_23_200525_create_custom_sequences_table.php

更多文档

您可以在源代码中以及位于 tests 目录中的测试中找到许多注释。