├── .gitignore ├── tests ├── config │ └── database.php ├── Unit │ ├── Models │ │ ├── Create │ │ │ └── CreateTest.php │ │ ├── Dummy.php │ │ └── Find │ │ │ └── FindTest.php │ └── Factories │ │ └── DummyFactory.php ├── database │ └── migrations │ │ └── create_dummy_table.php └── TestCase.php ├── composer.json ├── License.txt ├── phpunit.xml ├── README.md └── src └── HasCompositeKey.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | .phpunit.result.cache 3 | /.idea 4 | /logs 5 | /tests/database/*.sqlite -------------------------------------------------------------------------------- /tests/config/database.php: -------------------------------------------------------------------------------- 1 | [ 5 | 'driver' => 'sqlite', 6 | 'database' => 'tests/database/testing.sqlite', 7 | 'prefix' => '', 8 | ], 9 | ]; -------------------------------------------------------------------------------- /tests/Unit/Models/Create/CreateTest.php: -------------------------------------------------------------------------------- 1 | make(); 25 | $success = $model->save(); 26 | $this->assertTrue($success); 27 | } 28 | } -------------------------------------------------------------------------------- /tests/Unit/Factories/DummyFactory.php: -------------------------------------------------------------------------------- 1 | $this->faker->numberBetween(1, 9999999), 16 | 'key_2' => $this->faker->numberBetween(1, 9999999), 17 | 'name' => $this->faker->name(), 18 | 'email' => $this->faker->email(), 19 | 'phone' => $this->faker->phoneNumber(), 20 | 'city' => $this->faker->city(), 21 | ]; 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "thiagoprz/eloquent-composite-key", 3 | "description": "Eloquent Composite Key Support Package", 4 | "type": "library", 5 | "autoload": { 6 | "classmap": [ 7 | "src/" 8 | ], 9 | "psr-4": { 10 | "Thiagoprz\\CompositeKey\\": "src" 11 | } 12 | }, 13 | "autoload-dev": { 14 | "classmap": [ 15 | "tests/" 16 | ], 17 | "psr-4": { 18 | "Thiagoprz\\CompositeKey\\Tests\\": "tests" 19 | } 20 | }, 21 | "require": { 22 | "php": "^7.1 || ^8.0 || ^8.1" 23 | }, 24 | "require-dev": { 25 | "orchestra/testbench": "6", 26 | "squizlabs/php_codesniffer": "4.0.x-dev" 27 | }, 28 | "license": "MIT", 29 | "authors": [ 30 | { 31 | "name": "Thiago Przyczynski", 32 | "email": "przyczynski@gmail.com" 33 | } 34 | ], 35 | "minimum-stability": "dev" 36 | } 37 | -------------------------------------------------------------------------------- /tests/database/migrations/create_dummy_table.php: -------------------------------------------------------------------------------- 1 | unsignedBigInteger('key_1'); 18 | $table->unsignedBigInteger('key_2'); 19 | $table->string('name'); 20 | $table->string('email'); 21 | $table->string('phone'); 22 | $table->string('city'); 23 | $table->timestamps(); 24 | }); 25 | } 26 | 27 | /** 28 | * Reverse the migrations. 29 | * 30 | * @return void 31 | */ 32 | public function down() 33 | { 34 | Schema::dropIfExists('dummy'); 35 | } 36 | } -------------------------------------------------------------------------------- /License.txt: -------------------------------------------------------------------------------- 1 | Copyright 2022 Thiago Cezar Pereira Przyczynski 2 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 3 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 4 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | set('database.default', 'testbench'); 25 | $app['config']->set('database.connections.testbench', $database['sqlite']); 26 | $app['config']->set('app.debug', true); 27 | $app['config']->set('logging.default', 'daily'); 28 | $app['config']->set('logging.channels.daily.path', 'logs/testing.log'); 29 | unlink(__DIR__ . '/database/testing.sqlite'); 30 | touch(__DIR__ . '/database/testing.sqlite'); 31 | require_once __DIR__ . '/database/migrations/create_dummy_table.php'; 32 | (new \CreateDummyTable)->up(); 33 | } 34 | } -------------------------------------------------------------------------------- /tests/Unit/Models/Dummy.php: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | 24 | tests 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Eloquent Composite Key 2 | Package to enable composite key support on Eloquent Models. 3 | 4 | ## Installation 5 | Install it with composer: 6 | 7 | `composer require thiagoprz/eloquent-composite-key` 8 | 9 | ## Usage 10 | Define the primaryKey as an array and use the HasCompositeKey trait on your model class. 11 | ``` 12 | $key1, 31 | 'key_2' => $key2, 32 | ]); 33 | ... 34 | // Throws ModelNotFoundException 35 | $user = User::findOrFail([ 36 | 'key_1' => $key1, 37 | 'key_2' => $key2, 38 | ]); 39 | ... 40 | ``` 41 | 42 | The main idea of this package is to allow Laravel projects use composite keys on models despite Eloquent not supporting them officially (see https://laravel.com/docs/8.x/eloquent#composite-primary-keys). 43 | 44 | 45 | ## License 46 | [MIT](https://github.com/thiagoprz/eloquent-composite-key/blob/master/License.txt) -------------------------------------------------------------------------------- /tests/Unit/Models/Find/FindTest.php: -------------------------------------------------------------------------------- 1 | dummies = Dummy::factory()->count(2)->create(); 21 | } 22 | 23 | /** 24 | * @return void 25 | */ 26 | public function test_find_model_success() 27 | { 28 | $model = Dummy::find([ 29 | $this->dummies[0]->key_1, 30 | $this->dummies[0]->key_2, 31 | ]); 32 | $this->assertEquals($this->dummies[0]->getAttributes(), $model->getAttributes()); 33 | } 34 | 35 | /** 36 | * @return void 37 | */ 38 | public function test_find_wrong_arguments_failure() 39 | { 40 | $this->expectException(\TypeError::class); 41 | Dummy::find($this->dummies[0]->key_1); 42 | } 43 | 44 | /** 45 | * @return void 46 | */ 47 | public function test_find_or_fail_success() 48 | { 49 | $model = Dummy::findOrFail([ 50 | $this->dummies[0]->key_1, 51 | $this->dummies[0]->key_2, 52 | ]); 53 | $this->assertEquals($this->dummies[0]->getAttributes(), $model->getAttributes()); 54 | } 55 | 56 | /** 57 | * @return void 58 | */ 59 | public function test_find_or_fail_model_not_found_exception_failure() 60 | { 61 | $this->expectException(ModelNotFoundException::class); 62 | Dummy::findOrFail([ 63 | 1234, 64 | 21341, 65 | ]); 66 | } 67 | } -------------------------------------------------------------------------------- /src/HasCompositeKey.php: -------------------------------------------------------------------------------- 1 | getIncrementing()) { 22 | return array_merge([$this->getKeyName() => $this->getKeyType()], $this->casts); 23 | } 24 | return $this->casts; 25 | } 26 | 27 | public function getKeyName() 28 | { 29 | return $this->primaryKey; 30 | } 31 | 32 | public function getKey() 33 | { 34 | $fields = $this->getKeyName(); 35 | $keys = []; 36 | array_map(function ($key) use (&$keys) { 37 | $keys[] = $this->getAttribute($key); 38 | }, $fields); 39 | return $keys; 40 | } 41 | 42 | protected function getKeysForSaveQuery($query) 43 | { 44 | foreach ($this->primaryKey as $key) { 45 | $query->where($key, '=', $this->getAttribute($key)); 46 | } 47 | return $query; 48 | } 49 | 50 | protected function setKeysForSaveQuery($query) 51 | { 52 | $keys = $this->getKeyName(); 53 | return !is_array($keys) ? parent::setKeysForSaveQuery($query) : $query->where(function ($q) use ($keys) { 54 | foreach ($keys as $key) { 55 | $q->where($key, '=', $this->getAttribute($key)); 56 | } 57 | }); 58 | } 59 | 60 | public function getQueueableId() 61 | { 62 | return implode(':', array_map(fn($key) => $this->getAttribute($key), $this->getKeyName())); 63 | } 64 | 65 | 66 | public static function find(string|array $ids) 67 | { 68 | if (is_string($ids)) { 69 | $ids = explode(':', $ids); 70 | } 71 | 72 | $model = new static(); 73 | $keyNames = $model->getKeyName(); 74 | 75 | if (!is_array($ids) || count($ids) !== count($keyNames)) { 76 | return null; 77 | } 78 | 79 | return static::where(function ($query) use ($keyNames, $ids) { 80 | foreach ($keyNames as $index => $key) { 81 | $query->where($key, '=', $ids[$index]); 82 | } 83 | })->first(); 84 | } 85 | 86 | 87 | /** 88 | * Find model by primary key or throws ModelNotFoundException 89 | * 90 | * @param array $ids 91 | * @return mixed 92 | */ 93 | public static function findOrFail(array $ids) 94 | { 95 | $modelClass = get_called_class(); 96 | $model = new $modelClass(); 97 | $record = $model->find($ids); 98 | if (!$record) { 99 | throw new ModelNotFoundException; 100 | } 101 | return $record; 102 | } 103 | 104 | public function newQueryForRestoration($ids) 105 | { 106 | if (is_string($ids) && str_contains($ids, ':')) { 107 | $ids = explode(':', $ids); 108 | } 109 | 110 | if (is_array($ids)) { 111 | $keyNames = $this->getKeyName(); 112 | 113 | if (count($ids) !== count($keyNames)) { 114 | return parent::newQueryForRestoration($ids); 115 | } 116 | 117 | $query = $this->newQueryWithoutScopes(); 118 | foreach ($keyNames as $index => $key) { 119 | $query->where($key, '=', $ids[$index]); 120 | } 121 | 122 | return $query; 123 | } 124 | 125 | return parent::newQueryForRestoration($ids); 126 | } 127 | } 128 | --------------------------------------------------------------------------------