rozwell/composite-primary-keys

支持二进制列、可排队、隐式路由绑定、关系的最先进的复合主键包

v1.12.1 2020-09-08 20:47 UTC

README

Scrutinizer Code Quality codecov StyleCI CircleCI

关于

该库扩展了Laravel的Eloquent ORM,提供了对复合键的全面支持

使用方法

Laravel 5.5

composer require maksimru/composite-primary-keys ~0.14

Laravel 5.6+

composer require maksimru/composite-primary-keys ~1.0

简单地将 \MaksimM\CompositePrimaryKeys\Http\Traits\HasCompositePrimaryKey 特性添加到所需模型中

支持的功能

  • 增量和减量

  • 更新和保存查询

  • 二进制列

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

    如果 $hexBinaryColumns = false 或省略,则 $binaryKeyUser->user_id 将返回二进制值。在这种情况下应使用 BinaryKeyUser::find('BINARY_VALUE') 和 BinaryKeyUser::create(['id' => 'BINARY_VALUE'])

    可选自动将二进制值编码为其十六进制表示形式的能力

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

    如果 $hexBinaryColumns = true,则 $binaryKeyUser->user_id 将返回十六进制值。在这种情况下应使用 BinaryKeyUser::find('HEX_VALUE') 和 BinaryKeyUser::create(['id' => 'HEX_VALUE'])

    JSON 输出将在这两种情况下都包含十六进制值

  • 队列中的模型序列化(使用 Illuminate\Queue\SerializesModels 特性)

    作业

    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]);
        }
    }

    调度

    $model = TestUser::find([
        'user_id' => 1,
        'organization_id' => 100,
    ]);
    $this->dispatch(new TestJob($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')

    请求

    GET /binary-users/D9798CDF31C02D86B8B81CC119D94836___100

    响应

    {"user_id":"D9798CDF31C02D86B8B81CC119D94836","organization_id":"100","name":"Foo","user_id___organization_id":"D9798CDF31C02D86B8B81CC119D94836___100"}
  • 关系(仅支持此版本的 belongsTo 关系)

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

    将调用

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

    具有绑定 [ $testUser->referred_by_user_id, $testUser->referred_by_organization_id ]