承接 maksimru/composite-primary-keys 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

maksimru/composite-primary-keys

最新稳定版本:v1.12.0

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 ]

统计信息

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

GitHub 信息

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

其他信息

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