UUID
in Shopware 6?Shopware 6 uses UUID
as the primary key for tables. How can we generate them? That's easy:
use Shopware\Core\Framework\Uuid\Uuid;
$uuid = Uuid::randomHex();
You will get UUID
's like that:
b2797f53b1b84a82a70494270f2e9b1d
ceb13f897cb342588270a0eaec046254
ff83a318808f412c9f657e9182f6ed78
ef880f9328d4497ba905fbba903a538f
6a2c6945d1714056993a42432e118b20
143ef23ebd304e4a8e002f4532d9ca44
UUID
'sUniversally unique identifier (UUID
) is a random string containing the numbers 0
to 9
and the characters a
to f
with a length of 32
.
UUID
's?Good question! As everything in life there are a long list of pro's and con's. Let me point out some reasons on both sides.
Pro's
UUID
's are great for API-first platforms because it prevents Race Condition. Data can come from all over the place such as mobile apps, marketplaces, social shopping, or local points of sale. UUID
's are great to enable these channel's to generate a unique primary key by themselves, without conflicting with other channels. Simple numbers would easily conflict among themselvesUUID
's can be safe generated regardless of the database, since the primary key is not sequentialCon's
UUID
's can make heavy operations slow. It can potentially become a bottleneck in terms of performanceUUID
's take more space to be storedYou can dive more into the UUID
subject by checking some links: here, here and here.
See you on the next blogpost!