maksimru/composite-primary-keys

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


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 ]