swag_example, you'll find this name a few more times in here, so make sure to remember that one.
EntityDefinitionfor your table. As the name suggests, it defines your own entity, including its fields and name, the latter also represents the table name and therefore has to perfectly match.
<plugin root>/src/Core/Content/ExampleThis will also be the case for the
Entityclass itself, as well as the
EntityCollectionclass, but those are explained later in this guide.
ExampleDefinition.phpin the directory
<plugin root>/src/Core/Content/Example/ExampleDefinition.php. Below you'll see our example defininition, which is explained afterwards:
Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition, which enforces you to implement two methods:
getEntityNamereturns a string equal to your table name. In this example it is
swag_example. Keep in mind, that the return of your
getEntityNamemethod will be used for two cases:
defineFieldscontains all the fields, that your entity or table consists of.
activefield. Other than that, the other two columns
updated_atdon't have to be defined in your definition, since they're included by default. You're asked to return a
Shopware\Core\Framework\DataAbstractionLayer\FieldCollectioninstance here, which then has to contain an array of your fields. There's several field classes, e.g. an
Shopware\Core\Framework\DataAbstractionLayer\Field\StringField, which you have to create and pass into the
FieldCollection, so let's do that.
description, as well as a
Fieldclasses ask for two parameters, such as the
storageNameis written in snake_case, while the
propertyNamemust be written in lowerCamelCase.
addFlagscall on the
IdField. Those flags are like attributes to fields, such a required field being marked by using the
ExampleDefinitionto Shopware by registering your class in your
services.xmlfile and by using the
shopware.entity.definitiontag, because Shopware is looking for definitions this way. If your plugin does not have a
services.xmlfile yet or you don't know how that's done, head over to our guide about registering a custom service Add a custom class / service or our guide about the Dependency injection.
services.xmlas it should look like:
entityattribute, which has to contain the technical name of your entity, which you provided in your entity definition. In this case this must be
Entityclass, as well as a custom
EntityCollectionclass. This is not mandatory, but those will be replaced with generic classes otherwise.
ArrayEntityinstances, you'll get
ExampleEntityclass instances now if you were to read your data using the repository.
Entityclass, you do want to create your own
ExampleCollectionclass in the same directory as your
ExampleEntity. Extending from
Shopware\Core\Framework\DataAbstractionLayer\EntityCollection, it comes with a method called
getExpectedClass, which once again returns the fully qualified class name of the
Entityclass to be used. Go ahead and override this method and return your
ExampleEntityhere. Additionally, you can provide helper methods in your custom
EntityCollection, such as filtering the result set by certain conditions, but that's up to you.
ExampleDefinitionagain. This is done by overriding its