Skip to content

Commit 093db43

Browse files
Merge pull request #9 from verbanent/feature/custom-column-name
Allowed custom column name for primary key
2 parents bf8e62a + 255dc41 commit 093db43

40 files changed

+495
-133
lines changed

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
FROM php:fpm-buster
2-
RUN pecl install xdebug && docker-php-ext-enable xdebug
1+
FROM php:8-cli-alpine
2+
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" &&\
3+
php composer-setup.php &&\
4+
php -r "unlink('composer-setup.php');" &&\
5+
mv composer.phar /usr/local/bin/composer

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"description": "Ordered binary UUID in Laravel / Eloquent based on UUID version 1",
3737
"require": {
3838
"php": "^7.3|^8.0",
39-
"ramsey/uuid": "^3.8|^4.0"
39+
"ramsey/uuid": "^3.8|^4"
4040
},
4141
"require-dev": {
4242
"phpunit/phpunit": "^9",

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
version: "3.3"
22
services:
3-
php:
3+
ebu_php:
44
build: .
5-
container_name: php
5+
container_name: ebu_php
66
restart: unless-stopped
7+
tty: true
78
volumes:
89
- .:/var/www/html
9-
- ./docker/xdebug-local.ini:/usr/local/etc/php/conf.d/xdebug-local.ini

src/AbstractModel.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,4 @@ class AbstractModel extends Model
2727
* @var string
2828
*/
2929
protected $keyType = 'uuid';
30-
31-
/**
32-
* Column name for primary key.
33-
*
34-
* @var string
35-
*/
36-
protected $primaryKey = 'uuid';
37-
38-
/**
39-
* Allow to fill UUID column.
40-
*
41-
* @var array
42-
*/
43-
protected $fillable = ['uuid'];
4430
}

src/Exceptions/AccessedUnsetUuidPropertyException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66

77
class AccessedUnsetUuidPropertyException extends \RuntimeException
88
{
9+
//
910
}

src/Grammars/MySqlGrammar.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
use Illuminate\Support\Fluent;
99

1010
/**
11-
* Class change UUID type from default char(36) to binary(16).
11+
* Class changes the UUID type from default char(36) to binary(16).
1212
*/
1313
class MySqlGrammar extends IlluminateMySqlGrammar
1414
{
1515
/**
16-
* Create the column definition for a uuid type.
17-
*
18-
* @param \Illuminate\Support\Fluent
19-
*
20-
* @return string
16+
* Creates the column definition for the UUID type.
2117
*/
2218
protected function typeUuid(Fluent $column): string
2319
{

src/Traits/BinaryUuidSupportableTrait.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@ public static function bootBinaryUuidSupportableTrait(): void
6161
{
6262
static::creating(
6363
function (Model $model) {
64+
$uuid = $model->getKeyName();
65+
6466
/** @var Model|BinaryUuidSupportableTrait $model */
65-
if (!isset($model->attributes['uuid'])) {
66-
$model->uuid = $model->generateUuid();
67-
} elseif (Uuid::isValid($model->attributes['uuid'])) {
68-
$model->uuid = Uuid::fromString($model->attributes['uuid'])->getBytes();
69-
} elseif (is_string($model->attributes['uuid']) && strlen($model->attributes['uuid']) === 16) {
70-
$model->uuid = $model->attributes['uuid'];
67+
if (!isset($model->attributes[$uuid])) {
68+
$model->$uuid = $model->generateUuid();
69+
} elseif (Uuid::isValid($model->attributes[$uuid])) {
70+
$model->$uuid = Uuid::fromString($model->attributes[$uuid])->getBytes();
71+
} elseif (is_string($model->attributes[$uuid]) && strlen($model->attributes[$uuid]) === 16) {
72+
$model->$uuid = $model->attributes[$uuid];
7173
}
7274

7375
if (isset($model->readable) && $model->readable) {
@@ -101,13 +103,15 @@ public function generateUuid(): string
101103
*/
102104
public function uuid(): string
103105
{
104-
if (!isset($this->uuid)) {
106+
$uuid = $this->getKeyName();
107+
108+
if (!isset($this->$uuid)) {
105109
throw new AccessedUnsetUuidPropertyException(
106110
'Cannot get UUID property for not saved model'
107111
);
108112
}
109113

110-
return Uuid::fromBytes($this->uuid)->toString();
114+
return Uuid::fromBytes($this->$uuid)->toString();
111115
}
112116

113117
/**
@@ -119,6 +123,8 @@ public function uuid(): string
119123
*/
120124
public static function find(string $uuid): Model
121125
{
122-
return static::where('uuid', '=', Uuid::fromString($uuid)->getBytes())->firstOrFail();
126+
$uuidKey = app(static::class)->getKeyName();
127+
128+
return static::where($uuidKey, '=', Uuid::fromString($uuid)->getBytes())->firstOrFail();
123129
}
124130
}

src/Traits/ForeignBinaryUuidSupportableTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private static function isUuidValid(Model $model, string $uuidable): bool
118118
* @param string $columnName
119119
* @param string $uuid
120120
*
121-
* @return \Illuminate\Database\Eloquent\Collection
121+
* @return Collection
122122
*/
123123
public static function findByUuid(string $columnName, string $uuid): Collection
124124
{

tests/Example/AbstractExampleModel.php renamed to tests/Example/AbstractExampleIdModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
/**
1010
* Example class for traits tests.
1111
*/
12-
class AbstractExampleModel extends AbstractModel
12+
class AbstractExampleIdModel extends AbstractModel
1313
{
1414
/**
1515
* Table name.
1616
*
1717
* @var string
1818
*/
19-
protected $table = 'model_test';
19+
protected $table = 'model_test_id';
2020

2121
/**
2222
* Create rows in table without timestamps.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Verbanent\Uuid\Test\Example;
6+
7+
use Verbanent\Uuid\AbstractModel;
8+
9+
/**
10+
* Example class for traits tests.
11+
*/
12+
class AbstractExampleUuidModel extends AbstractModel
13+
{
14+
/**
15+
* Table name.
16+
*
17+
* @var string
18+
*/
19+
protected $table = 'model_test_uuid';
20+
21+
/**
22+
* Create rows in table without timestamps.
23+
*
24+
* @var bool
25+
*/
26+
public $timestamps = false;
27+
28+
/**
29+
* Column name for primary key.
30+
*
31+
* @var string
32+
*/
33+
protected $primaryKey = 'uuid';
34+
35+
/**
36+
* Allow to fill UUID column.
37+
*
38+
* @var array
39+
*/
40+
protected $fillable = ['uuid'];
41+
}

0 commit comments

Comments
 (0)