定制 maksimru/composite-primary-keys 二次开发

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

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

maksimru/composite-primary-keys

Composer 安装命令:

composer require maksimru/composite-primary-keys

包简介

Most advanced composite primary keys package with support of: binary columns, queueable, implicit route binding, relations

README 文档

README

Scrutinizer Code Quality codecov StyleCI CircleCI

About

Library extends Laravel's Eloquent ORM with pretty full support of composite keys

Usage

Laravel 5.5

composer require maksimru/composite-primary-keys ~0.14

Laravel 5.6+

composer require maksimru/composite-primary-keys ~1.0

Simply add \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey trait into required models

Features Support

  • increment and decrement

  • update and save query

  • binary columns

    class BinaryKeyUser extends Model
    {
        use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;
    
        protected $binaryColumns = [
            'user_id'
        ];
    
        protected $primaryKey = 'user_id';
    }

    With $hexBinaryColumns = false or omitted, $binaryKeyUser->user_id will return binary value. BinaryKeyUser::find('BINARY_VALUE') and BinaryKeyUser::create(['id' => 'BINARY_VALUE']) should be used in this case

    Optional ability to automatically encode binary values to their hex representation:

    class BinaryKeyUser extends Model
    {
      use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;
    
      protected $binaryColumns = [
          'user_id'
      ];
    
      protected $primaryKey = 'user_id';
    
      protected $hexBinaryColumns = true;
    }

    With $hexBinaryColumns = true, $binaryKeyUser->user_id will return hex value. BinaryKeyUser::find('HEX_VALUE') and BinaryKeyUser::create(['id' => 'HEX_VALUE']) should be used in this case

    JSON output will contain hex values in both cases

  • model serialization in queues (with Illuminate\Queue\SerializesModels trait)

    Job:

    class TestJob implements ShouldQueue
    {
        use Queueable, SerializesModels;
    
        private $model;
    
        /**
         * Create a new job instance.
         */
        public function __construct(TestUser $testUser)
        {
            $this->model = $testUser;
        }
    
        /**
         * Execute the job.
         */
        public function handle()
        {
            $this->model->update(['counter' => 3333]);
        }
    }

    Dispatch:

    $model = TestUser::find([
        'user_id' => 1,
        'organization_id' => 100,
    ]);
    $this->dispatch(new TestJob($model));
  • route implicit model binding support

    Model:

    class TestBinaryUser extends Model
    {
        use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;
        
        protected $table = 'binary_users';
        
        public $timestamps = false;
        
        protected $binaryColumns = [
          'user_id'
        ];
        
        protected $primaryKey = [
          'user_id',
          'organization_id',
        ];
    }

    routes.php:

    $router->get('binary-users/{binaryUser}', function (BinaryUser $binaryUser) {
        return $binaryUser->toJson();
    })->middleware('bindings')

    request:

    GET /binary-users/D9798CDF31C02D86B8B81CC119D94836___100

    response:

    {"user_id":"D9798CDF31C02D86B8B81CC119D94836","organization_id":"100","name":"Foo","user_id___organization_id":"D9798CDF31C02D86B8B81CC119D94836___100"}
  • relations (only belongsTo relation is supported in this version)

    class TestUser extends Model
    {
        use \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey;
    
        protected $table = 'users';
    
        protected $primaryKey = [
            'user_id',
            'organization_id',
        ];
    
        public function referrer()
        {
            return $this->belongsTo(TestUser::class, [
                'referred_by_user_id',
                'referred_by_organization_id'
            ], [
                'user_id',
                'organization_id',
            ]);
        }
    }
    
    $referrer_user = $testUser->referrer()->first();

    will call

    select * from "users" where (("user_id" = ? and "organization_id" = ?)) limit 1

    with bindings [ $testUser->referred_by_user_id, $testUser->referred_by_organization_id ]

maksimru/composite-primary-keys 适用场景与选型建议

maksimru/composite-primary-keys 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.18k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2019 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 maksimru/composite-primary-keys 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 15.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-03