Adding UUID to the DB model
Edit on GitHubSpryker 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.
- Require the composer dependency:
composer require spryker/uuid-behavior
- 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.
- Update the database schema and regenerate PHP models:
console propel:install
- Verify that the
uuidcolumn is present in the database table. Insert a new entity to confirm that UUIDs are generated automatically.
For tables with existing data
- Require the composer dependency:
composer require spryker/uuid
- Add
\Spryker\Zed\Uuid\Communication\Console\UuidGeneratorConsoleto\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(),
...
- Execute the
uuid:generateconsole command for each affected table:
console uuid:generate <module> <table-name>
- Verify that all
uuidvalues in the table<table-name>are filled with values.
Thank you!
For submitting the form