There are several cases where you may want to generate a unique key. For example:
IDattribute in your HTML output
- key for partial cache
If this is for a public-facing purpose, it is typically considered bad practice to expose the database ID,
as this provides unnecessary information to would-be attackers. Instead, for
DataObject subclasses, you
can use the
This can be done simply by including following code in your template:
The unique key generation can be altered in two ways:
- you can provide extra data to be used when generating a key via an extension
- you can inject over the key generation service and write your own custom code.
cacheKeyComponent extension hook is invoked from
Simply implement a
cacheKeyComponent() method in an
Extension applied to the
DataObject class you want to affect.
The method must return a string, which will be used when generating a unique key for the record.
Some cases where this is used in supported modules already are:
- versions - an object in different version stages needs to have different unique keys for each stage
- locales - an object in different locales needs to have different unique keys for each locale
See Extensions and DataExtensions for more information about implementing and applying extensions.
UniqueKeyService is used by default but you can use injector to override it with your custom service. For example:
SilverStripe\Core\Injector\Injector: SilverStripe\ORM\UniqueKey\UniqueKeyService: class: App\Service\MyCustomService
Your custom service has to implement
A use case for implementing your own service might be to produce a UUID for each record.