maksimru / composite-primary-keys
Most advanced composite primary keys package with support of: binary columns, queueable, implicit route binding, relations
Requires
- php: >=7.1
- ext-json: *
- illuminate/cache: ^5.6
- illuminate/contracts: ^5.6
- illuminate/queue: ^5.6
- illuminate/redis: ^5.6
- illuminate/session: ^5.6
- illuminate/support: ^5.6
Requires (Dev)
- barryvdh/laravel-debugbar: ^3.1
- fzaninotto/faker: ~1.4
- laravel/framework: ^5.6
- mockery/mockery: 0.9.* || 1.3.*
- orchestra/database: ~3.0
- orchestra/testbench: ~3.6
- orchestra/testbench-browser-kit: ~3.6
- phpunit/phpunit: 7
- dev-master
- v1.12.0
- v1.11.0
- 1.10.0
- v1.08.1
- v1.08
- v1.07
- v1.05
- v1.04.5
- v1.04.4
- v1.04.3
- v1.04.2
- v1.04.1
- v1.04
- v1.03
- v1.02
- v1.01
- v1.0
- v0.14
- v0.13
- v0.12
- v0.11
- v0.10
- dev-dependabot/add-v2-config-file
- dev-fix/increments-and-dep-issue
- dev-analysis-jLWE4r
- dev-analysis-m4olep
- dev-analysis-kaWQLG
- dev-analysis-GDKQVA
- dev-analysis-3wyDA4
- dev-analysis-9m3pv9
- dev-analysis-RvD9en
- dev-analysis-WNl32O
- dev-analysis-vQkGrg
- dev-analysis-5Z9D3l
- dev-fix/missing-relations
- dev-analysis-zGBPxo
- dev-fix/binary-model-restoration
- dev-analysis-z3LbmP
- dev-analysis-q19PK3
- dev-analysis-qxVZNa
- dev-fix/optional-hex-keys
- dev-analysis-XkwGp9
- dev-analysis-8mANnb
- dev-feature/auto-binary-keys
- dev-analysis-qyaQ5G
- dev-laravel-5.6
- dev-analysis-zY4bEv
- dev-analysis-qgl6Kw
This package is auto-updated.
Last update: 2024-10-30 01:37:15 UTC
README
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 ]