Adding UUID to the DB model

Edit on GitHub

Spryker can automatically generate a UUID column based on row data. Expose only UUIDs through customer-facing APIs, such as the Storefront API and Glue API.

  1. Require the composer dependency:
composer require spryker/uuid-behavior
  1. Extend your <table-name> Propel model definition with:
        <column name="uuid" required="false" type="VARCHAR" size="36"/>

        <behavior name="uuid">
            <parameter
                name="key_columns"
                value="<column list, dot-separated, e.g. id_table_name.some_column.>"
        />
        </behavior>

The column list must contain columns from the same table, separated by dots (.). The UUID is generated from a string that concatenates the dot-separated values of these columns. Include the primary key (id_…) in the list, because UUID version 5 (UUIDv5) generates the same UUID for the same combined key-column values.

Prerequisite

Add a uuid column of type string with a length of at least 36 characters. Otherwise, insert operations into this table fail.

  1. Update the database schema and regenerate PHP models:
console propel:install
  1. Verify that the uuid column is present in the database table. Insert a new entity to confirm that UUIDs are generated automatically.

For tables with existing data

  1. Require the composer dependency:
composer require spryker/uuid
  1. Add \Spryker\Zed\Uuid\Communication\Console\UuidGeneratorConsole to \Pyz\Zed\Console\ConsoleDependencyProvider::getConsoleCommands:
namespace Pyz\Zed\Console;

...
use Spryker\Zed\Uuid\Communication\Console\UuidGeneratorConsole;
...

class ConsoleDependencyProvider extends SprykerConsoleDependencyProvider

    ...
    protected function getConsoleCommands(Container $container): array
            ...
            new UuidGeneratorConsole(),
            ...

  1. Execute the uuid:generate console command for each affected table:
console uuid:generate <module> <table-name> 
  1. Verify that all uuid values in the table <table-name> are filled with values.