Field inheritance

Overview

In this guide you'll learn how to create inherited fields for your entities. Field inheritance allows you to tell Shopware which fields should inherit values from a parent entity.

Prerequisites

This guide is built upon the Plugin Base Guide, but any plugin will work here. Just note that all examples are using the plugin mentioned above.
You also should have a look at our Adding custom complex data guide, since this guide is built upon it.

Inherit a field

To start using inheritance, we have to update our definition and database.
  1. 1.
    Make inheritable fields nullable in the database
  2. 2.
    Add the ParentFkField, ParentAssociationField, ChildrenAssociationField in your definition
  3. 3.
    Enable inheritance by overwriting isInheritanceAware()
  4. 4.
    Flag fields as inheritable
  5. 5.
    Add getters and setters to the entity class

Make fields nullable

The first thing we need to do is to make all our fields that we want to make inheritable nullable in our migration. If you lack knowledge about migrations, have a look at our database migrations guide. We also need a 'parent_id' field for the parent reference.
1
ALTER TABLE `swag_example` ADD `parent_id` BINARY(16) NULL;
2
ALTER TABLE `swag_example` MODIFY `description` VARCHAR(255) NULL;
Copied!
To avoid creating the column incorrectly, you can simply use the Shopware\Core\Framework\Migration\InheritanceUpdaterTrait in your migrations for new fields:
<plugin root>/src/Migration/Migration1615363012AddInheritanceColumnToExample.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Migration;
4
5
use Doctrine\DBAL\Connection;
6
use Shopware\Core\Framework\Migration\InheritanceUpdaterTrait;
7
use Shopware\Core\Framework\Migration\MigrationStep;
8
9
class Migration1615363012AddInheritanceColumnToExample extends MigrationStep
10
{
11
use InheritanceUpdaterTrait;
12
13
public function getCreationTimestamp(): int
14
{
15
return 1615363012;
16
}
17
18
public function update(Connection $connection): void
19
{
20
$query = <<<SQL
21
ALTER TABLE `swag_example`
22
ADD `parent_id` BINARY(16) NULL,
23
MODIFY `description` VARCHAR(255) NULL;
24
SQL;
25
26
$connection->executeStatement($query);
27
$this->updateInheritance($connection, 'swag_example', 'example_field');
28
}
29
30
public function updateDestructive(Connection $connection): void
31
{
32
}
33
}
Copied!

Add the ParentFkField and the associations

After we've made all our fields nullable, we still need to add the following fields to our definition: Shopware\Core\Framework\DataAbstractionLayer\Field\ParentFkField, Shopware\Core\Framework\DataAbstractionLayer\Field\ParentAssociationField and Shopware\Core\Framework\DataAbstractionLayer\Field\ChildrenAssociationField.
  • ParentFkField: Is the foreign key, that references the parent's id.
  • ParentAssociationField: Field that the DAL knows where to load the parent association from.
  • ChildrenAssociationField: Field that the DAL knows where to load the children association from.
In default, ParentFkField points to a parent_id column in the database. All these fields must refer to our definition by using self::class. The ParentAssociationField has as its second parameter the referenceField, which in our case is id. Below you can find an example of how it should then look.
<plugin root>/src/Core/Content/Example/ExampleDefinition.php
1
protected function defineFields(): FieldCollection
2
{
3
return new FieldCollection([
4
...
5
6
new ParentFkField(self::class),
7
new ParentAssociationField(self::class, 'id'),
8
new ChildrenAssociationField(self::class),
9
10
...
11
]);
12
}
Copied!

Allow inheritance

Now we need to enable inheritance by overriding the isInheritanceAware method in our definition, which must then return true.
<plugin root>/src/Core/Content/Example/ExampleDefinition.php
1
public function isInheritanceAware(): bool
2
{
3
return true;
4
}
Copied!

Flag fields as inheritable

After we've enabled inheritance for our definition, we need to add theShopware\Core\Framework\DataAbstractionLayer\Field\Flag\Inherited flag to all the fields in our definition that should be inherited.
<plugin root>/src/Core/Content/Example/ExampleDefinition.php
1
protected function defineFields(): FieldCollection
2
{
3
return new FieldCollection([
4
(new IdField('id', 'id'))->addFlags(new Required(), new PrimaryKey()),
5
6
new ParentFkField(self::class),
7
new ParentAssociationField(self::class, 'id'),
8
new ChildrenAssociationField(self::class),
9
10
(new StringField('name', 'name'))->addFlags(new Inherited()),
11
(new StringField('description', 'description'))->addFlags(new Inherited()),
12
(new BoolField('active', 'active'))->addFlags(new Inherited()),
13
]);
14
}
Copied!

Add getters and setters to the entity class

The last thing we need to do is add our new fields to our entity class.
<plugin root>/src/Core/Content/Example/ExampleEntity.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\BasicExample\Core\Content\Example;
4
5
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
6
use Shopware\Core\Framework\DataAbstractionLayer\EntityIdTrait;
7
8
class ExampleEntity extends Entity
9
{
10
...
11
12
protected ?self $parent = null;
13
14
protected ?string $parentId;
15
16
protected ?ExampleCollection $children = null;
17
18
...
19
20
public function getParent(): ?ExampleEntity
21
{
22
return $this->parent;
23
}
24
25
public function setParent(ExampleEntity $parent): void
26
{
27
$this->parent = $parent;
28
}
29
30
public function getParentId(): ?string
31
{
32
return $this->parentId;
33
}
34
35
public function setParentId(?string $parentId): void
36
{
37
$this->parentId = $parentId;
38
}
39
40
public function getChildren(): ?ExampleCollection
41
{
42
return $this->children;
43
}
44
45
public function setChildren(ExampleCollection $children): void
46
{
47
$this->children = $children;
48
}
49
}
Copied!

Translations

This concept also supports translations. Given a parent/child entity with an inherited language (de-CH inherits from de-DE), the inheritance system will try to look up the values in following order:
  1. 1.
    Child (de-CH)
  2. 2.
    Child (de-DE)
  3. 3.
    Parent (de-CH)
  4. 4.
    Parent (de-DE)
If an inheritance is not found, the next translation in the chain above will be used.

Enable translation inheritance

Assuming our definition is already aware of inheritance, we have to update our definition and add the Inherited flag to our translated fields and the translation association.
1
(new TranslatedField('name'))->addFlags(new Inherited()),
2
(new TranslationsAssociationField(ExampleTranslationDefinition::class))->addFlags(new Inherited()),
Copied!