~/docs
Resonate
Resonate
4.x 4.0.0, current 3.x 3.0.1 2.x 2.0.1 1.x 1.3.0
UUID Library
Toggle sidebar
UUID Library
webpatser/uuid 2.0.1
You are reading the 2.x documentation
The current line is 4.x. Read this page for 4.x

UUID Library 2.x

Microsoft SQL Server

Convert between standard UUIDs and SQL Server uniqueidentifier values with importFromSqlServer(), toSqlServer() and toSqlServerBinary().

SQL Server stores a uniqueidentifier with mixed endianness. The first three groups (4, 2 and 2 bytes) are byte-reversed compared to the RFC 4122 layout; the last 8 bytes are unchanged. The library has three methods for this conversion.

Method Description
Uuid::importFromSqlServer(string $sqlServerGuid): self SQL Server GUID string to a standard Uuid
$uuid->toSqlServer(): string Uppercase GUID string in SQL Server byte order
$uuid->toSqlServerBinary(): string 16 bytes in SQL Server byte order

Import from SQL Server

use Webpatser\Uuid\Uuid;
 
$uuid = Uuid::importFromSqlServer('825B076B-44EC-E511-80DC-00155D0ABC54');
 
echo $uuid->string; // 6b075b82-ec44-11e5-80dc-00155d0abc54

Anything that does not contain 32 hex digits throws Exception('Invalid SQL Server GUID format.').

Export to SQL Server

$uuid = Uuid::import('6b075b82-ec44-11e5-80dc-00155d0abc54');
 
$sqlGuid = $uuid->toSqlServer(); // "825B076B-44EC-E511-80DC-00155D0ABC54"
$sqlBin = $uuid->toSqlServerBinary(); // 16 bytes

Round trip

$original = Uuid::v4();
 
$back = Uuid::importFromSqlServer($original->toSqlServer());
 
Uuid::compare($original->string, $back->string); // true

Removed in 2.0.0

isSqlServerFormat() is gone. It only validated the string format and could not detect the byte order. See Upgrading from 1.x.