enea/laravel-sequenceable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

enea/laravel-sequenceable

最新稳定版本:v5.1.1

Composer 安装命令:

composer require enea/laravel-sequenceable

包简介

Facilitates the generation and autocompletion of a sequential value in the database

README 文档

README

Build Status Software License

Laravel Sequenceable is a library to generate and manage sequences for laravel models.

// 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);

Installation

Laravel Sequenceable requires PHP 8.1.

To get the latest version, simply require the project using Composer:

composer require vaened/laravel-sequenceable

Now. Publish the configuration file.

php artisan vendor:publish --tag='sequenceable'

And finally run migrations.

php artisan migrate

Basic Usage

Getting started with the library is as simple as using the Sequenceable trait and implementing the SequenceableContract interface, after that you only need to specify the sequences you want to generate.

namespace App;

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

class Document extends Model implements SequenceableContract
{
    use Sequenceable;

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

Advanced

We exemplify all the options to generate a sequence with the case of a payment document.

  • To start, we need a column to store the sequence, and for this we will use a column called number.

    public function sequencesSetup(): array
    {
        return [ Serie::lineal('document_number') ];
    }
  • Now that we have the column defined, we realize that we need to create a separate sequence for each type of document. For this problem, the library offers the possibility of adding an scope for the column.

    public function sequencesSetup(): array
    {
        return [ 
            Serie::lineal('document_number')->scope($this->type())
        ];
    }
    
    protected function type(): string
    {
        return $this->payment_document_type;
    }
  • Everything is fine, but now we want the sequences not to be saved in a numeric value, but instead to be a text string with a fixed length of 10.

    public function sequencesSetup(): array
    {
        return [ 
            Serie::lineal('document_number')->scope($this->type())->length(10)
        ];
    }
  • Concluding, we could also say that we do not want to use the default table for sequences and we need a special table to store the payment sequences, for this you have to create your own sequence table.

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

    We can wrap a block of sequences using the class Vaened\Sequenceable\Wrap::create

List

To retrieve all the sequences of a model, you can use the Vaened\Sequenceable\Facades\Succession facade which is linked to the Vaened\Sequenceable\Succession class.

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

This returns an instance of Vaened\Sequenceable\SequenceCollection. With which you can do things like:

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

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

Config

You can change the default sequence model of config\sequenceable.php in the model key.

<?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' => \Vaened\Sequenceable\Model\Sequence::class,
];

Or explicitly specify the model you want to use with certain fields, you can achieve this from the configuration of the sequences in your model.

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

Customize

if you already have a model to store your sequences, you need to implement the Vaened\Sequenceable\Contracts\SequenceContract interface, or extend the default model Vaened\Sequenceable\Model\Sequence.

In case you have your own sequence model, there are some fields that you should store in its sequence table:

  1. The column ID, and this is obtained by concatenating the column name and scope.
  2. The name of the table to which the sequence belongs.
  3. An integer type sequence.

Example

To better exemplify this, we will use the default Sequence model.

This model comes with a default configuration.

id sequence source column_id created_at updated_at
e4910d63 1 documents document_number.invoce 2020-07-03 18:40:44 2020-07-03 18:40:44

Migration

The table structure has the required fields, you can see the migration in CreateSequencesTable

Column Description Required
id It is generated on the basis of the union of the table, column and scope
sequence Stores the last value in the sequence
source Stores the table name
column_id Concatenated name and scope
created_at Indicates the date and time the sequence was created
updated_at Indicates the last time the sequence is updated

You can see another example of this in the test folder and look for the files CustomSequence.php and migrations/2017_04_23_200525_create_custom_sequences_table.php

More documentation

You can find a lot of comments within the source code as well as the tests located in the tests directory.

enea/laravel-sequenceable 适用场景与选型建议

enea/laravel-sequenceable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16.08k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 16.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-26