Flags

Version

6.0.0 or newer

Table of contents

Flags are attributes to a field in a definition. They provide additional information, which is not field type specific.

Flags for fields

ClassPurpose
PrimaryKeyThe field is part of the primary key for this entity
WriteProtectedWriting to this field is only allowed if the configured context is given
RuntimeThe value of the field won't be hydrated by the DataAbstractionLayer and must be filled in manually via extensions.
ExtensionThe value of the field will be handled as an extension and gets a data struct in the main struct.
RequiredThe field is required when creating the entity.
InheritedThe field is part of the parent/child concept and may receive the value of its parent.
ReverseInheritedReverse side flag for relations that point to a definition with inheritance enabled.
SearchRankingThe field will be weighted differently for score queries.
ReadProtectedThe field will be restricted for one or multiple sources (SalesChannelApiSource, AdminApiSource)

Flags exclusive for association

ClassPurpose
RestrictDeleteThe entity cannot be deleted unless their relations are removed
CascadeDeleteRelated entities will be deleted via constraints

Using flags

You have to add the flags to fields in your definition in order to use them. You can even modify the field's flags by creating definition extensions.

(new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required())

You can also use setFlags to overwrite the Default Flags which could be set. Be Careful to not overwrite Essential Flags for a specific Field

(new IdField('id', 'id'))->setFlags(new Required())

PrimaryKey

(new IdField('id', 'id'))->addFlags(new PrimaryKey())

The field is part of the primary key for this entity.

This flag does not have any parameters.

WriteProtected

(new StringField('file_extension', 'fileExtension'))->addFlags(new WriteProtected(SourceContext::SYSTEM))

In some cases, you want to restrict the write access to individual fields, so that they can't be manipulated. For example, if you have to run some custom logic before you can update a field's value.

This can be accomplished with the WriteProtected flag. If you add this flag, you have to define the source context, that the call needs to be.

You can temporarily change your context and execute your code:

$context->scope(SourceContext::SYSTEM, function (Context $context) {
    // do stuff in SYSTEM context
});

Runtime

(new StringField('url', 'url'))->addFlags(new Runtime())

Defines that the data of the field will be loaded at runtime by an event subscriber or other service class. Mainly used in entity extensions for plugins or not directly fetchable associations.

This flag does not have any parameters.

Extension

(new StringField('url', 'url'))->addFlags(new Extension())

Defines that the data of this field is stored in the Entity::$extension property and are not part of the struct itself.

This flag does not have any parameters.

Required

(new StringField('url', 'url'))->addFlags(new Required())

The field is required when creating the entity.

This flag does not have any parameters.

Inherited

(new LongTextField('description', 'description'))->addFlags(new Inherited())

The field is part of the parent/child concept and may receive the value of its parent.

This flag does not have any parameters.

ReverseInherited

(new OneToManyAssociationField('products', ProductDefinition::class, 'tax_id'))->addFlags(new ReverseInherited('tax'))

Reverse side flag for relations that point to a definition with inheritance enabled. The first parameter $name must be the association field name in the foreign definition.

SearchRanking

(new StringField('name', 'name'))->addFlags(new SearchRanking(5))

Defines the weight for a search query on the entity for this field. The first parameter $ranking defines the multiplier which will be applied. The multiplier can also lessen the value, too.

RestrictDelete

(new OneToManyAssociationField('products', ProductDefinition::class, 'tax_id'))->addFlags(new RestrictDelete())

Associated data with this flag, restricts the delete of the entity in case that a record with the primary key exists.

This flag does not have any parameters.

CascadeDelete

(new OneToManyAssociationField('media', ProductMediaDefinition::class, 'product_id'))->addFlags(new CascadeDelete())

In case the referenced association data will be deleted, the related data will be deleted too

This flag does not have any parameters.