Skip to content

Commit

Permalink
Merge pull request #26 from westphalen/feature/manual-ids
Browse files Browse the repository at this point in the history
UUID is only generated when not set manually.
  • Loading branch information
alsofronie authored Mar 12, 2017
2 parents 39d0026 + 60b7d34 commit 6de2df0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
16 changes: 9 additions & 7 deletions src/Uuid32ModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public function getIncrementing()
public static function bootUuid32ModelTrait()
{
static::creating(function ($model) {

// This is necessary because on \Illuminate\Database\Eloquent\Model::performInsert
// will not check for $this->getIncrementing() but directly for $this->incrementing
$model->incrementing = false;
$uuidVersion = (!empty($model->uuidVersion) ? $model->uuidVersion : 4); // defaults to 4
$uuid = Uuid::generate($uuidVersion);
$model->attributes[$model->getKeyName()] = str_replace('-', '', $uuid->string);
// Only generate UUID if it wasn't set by already.
if (!isset($model->attributes[$model->getKeyName()])) {
// This is necessary because on \Illuminate\Database\Eloquent\Model::performInsert
// will not check for $this->getIncrementing() but directly for $this->incrementing
$model->incrementing = false;
$uuidVersion = (!empty($model->uuidVersion) ? $model->uuidVersion : 4); // defaults to 4
$uuid = Uuid::generate($uuidVersion);
$model->attributes[$model->getKeyName()] = str_replace('-', '', $uuid->string);
}
}, 0);
}
}
15 changes: 9 additions & 6 deletions src/UuidBinaryModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ public function getIncrementing()
public static function bootUuidBinaryModelTrait()
{
static::creating(function ($model) {
// This is necessary because on \Illuminate\Database\Eloquent\Model::performInsert
// will not check for $this->getIncrementing() but directly for $this->incrementing
$model->incrementing = false;
$uuidVersion = (!empty($model->uuidVersion) ? $model->uuidVersion : 4); // defaults to 4
$uuid = Uuid::generate($uuidVersion);
$model->attributes[$model->getKeyName()] = (property_exists($model, 'uuidOptimization') && $model::$uuidOptimization ? $model::toOptimized($uuid->string) : $uuid->bytes);
// Only generate UUID if it wasn't set by already.
if (!isset($model->attributes[$model->getKeyName()])) {
// This is necessary because on \Illuminate\Database\Eloquent\Model::performInsert
// will not check for $this->getIncrementing() but directly for $this->incrementing
$model->incrementing = false;
$uuidVersion = (!empty($model->uuidVersion) ? $model->uuidVersion : 4); // defaults to 4
$uuid = Uuid::generate($uuidVersion);
$model->attributes[$model->getKeyName()] = (property_exists($model, 'uuidOptimization') && $model::$uuidOptimization ? $model::toOptimized($uuid->string) : $uuid->bytes);
}
}, 0);
}

Expand Down
16 changes: 9 additions & 7 deletions src/UuidModelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ public function getIncrementing()
public static function bootUuidModelTrait()
{
static::creating(function ($model) {

// This is necessary because on \Illuminate\Database\Eloquent\Model::performInsert
// will not check for $this->getIncrementing() but directly for $this->incrementing
$model->incrementing = false;
$uuidVersion = (!empty($model->uuidVersion) ? $model->uuidVersion : 4); // defaults to 4
$uuid = Uuid::generate($uuidVersion);
$model->attributes[$model->getKeyName()] = $uuid->string;
// Only generate UUID if it wasn't set by already.
if (!isset($model->attributes[$model->getKeyName()])) {
// This is necessary because on \Illuminate\Database\Eloquent\Model::performInsert
// will not check for $this->getIncrementing() but directly for $this->incrementing
$model->incrementing = false;
$uuidVersion = (!empty($model->uuidVersion) ? $model->uuidVersion : 4); // defaults to 4
$uuid = Uuid::generate($uuidVersion);
$model->attributes[$model->getKeyName()] = $uuid->string;
}
}, 0);
}
}

0 comments on commit 6de2df0

Please sign in to comment.