Using flags

Overview

In this guide you'll learn how to use flags of the DAL but this guide will not explain all flags and its purpose.

Prerequisites

In order to use flags in your entities for your plugin, you first need a plugin as base. Therefore, you can refer to the Plugin Base Guide.
You should also have a look at the Flags reference to understand what each flag is used for. Furthermore you should know how entities work, therefore you can head over to our Adding custom complex data as entity guide.

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 entity extensions. It is also possible to use multiple flags comma separated.

Single flag example

1
(new IdField('id', 'id'))->addFlags(new PrimaryKey())
Copied!

Multiple flags example

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

Overwrite default flags

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.
1
(new IdField('id', 'id'))->setFlags(new Required())
Copied!

Example entity

Below you can find an example implementation in an entity where we use flags.
<plugin root>/src/Core/Content/Example/ExampleDefinition.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Core\Content\Example;
4
5
use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition;
6
use Shopware\Core\Framework\DataAbstractionLayer\FieldCollection;
7
8
class ExampleDefinition extends EntityDefinition
9
{
10
public const ENTITY_NAME = 'swag_example';
11
12
public function getEntityName(): string
13
{
14
return self::ENTITY_NAME;
15
}
16
17
protected function defineFields(): FieldCollection
18
{
19
return new FieldCollection([
20
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
21
(new StringField('name', 'name')),
22
(new StringField('description', 'description')),
23
(new BoolField('active', 'active'))
24
]);
25
}
26
}
Copied!