Skip to content

Commit

Permalink
Merge pull request #4 from mlewis-everley/mle-1
Browse files Browse the repository at this point in the history
Ensure base table name is always correctly found
  • Loading branch information
lekoala authored Oct 24, 2023
2 parents 89dae38 + 9313600 commit 54cddc9
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/UuidExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,30 @@

class UuidExtension extends DataExtension
{
const UUID_FIELD = 'Uuid';
const UUID_BINARY_FORMAT = 'binary';
const UUID_STRING_FORMAT = 'string';
const UUID_BASE62_FORMAT = 'base62';

private static $db = [
"Uuid" => DBUuid::class,
self::UUID_FIELD => DBUuid::class,
];

private static $indexes = [
"Uuid" => true,
self::UUID_FIELD => true,
];

protected function getBaseTableName(): string
{
$schema = DataObjectSchema::create();
$table = $schema->tableForField(
get_class($this->getOwner()),
self::UUID_FIELD
);

return $table;
}

/**
* Assign a new uuid to this record. This will overwrite any existing uuid.
*
Expand All @@ -35,8 +48,7 @@ public function assignNewUuid($check = true)
{
$uuid = Uuid::uuid4();
if ($check) {
$schema = DataObjectSchema::create();
$table = $schema->tableForField(get_class($this->owner), 'Uuid');
$table = $this->getBaseTableName();
do {
$this->owner->Uuid = $uuid->getBytes();
// If we have something, keep checking
Expand Down Expand Up @@ -88,7 +100,10 @@ public static function getByUuid($class, $value, $format = null)
return false;
}
// Fetch the first record and disable subsite filter in a similar way as asking by ID
$q = $class::get()->filter('Uuid', $uuid->getBytes())->setDataQueryParam('Subsite.filter', false);
$q = $class::get()->filter(
self::UUID_FIELD,
$uuid->getBytes()
)->setDataQueryParam('Subsite.filter', false);
return $q->first();
}

Expand Down Expand Up @@ -127,12 +142,11 @@ public function UuidSegment()
$uuid = $this->assignNewUuid();
// Make a quick write without using orm
if ($this->owner->ID) {
$schema = new DataObjectSchema;
$table = $schema->tableName(get_class($this->owner));
$table = $this->getBaseTableName();
DB::prepared_query("UPDATE $table SET Uuid = ? WHERE ID = ?", [$uuid, $this->owner->ID]);
}
}
return $this->owner->dbObject('Uuid')->Base62();
return $this->owner->dbObject(self::UUID_FIELD)->Base62();
}

public function updateCMSFields(FieldList $fields)
Expand Down

0 comments on commit 54cddc9

Please sign in to comment.