diff --git a/guides/plugins/plugins/framework/data-handling/entities-via-attributes.md b/guides/plugins/plugins/framework/data-handling/entities-via-attributes.md index 6599c8a49..8e466a852 100644 --- a/guides/plugins/plugins/framework/data-handling/entities-via-attributes.md +++ b/guides/plugins/plugins/framework/data-handling/entities-via-attributes.md @@ -7,18 +7,24 @@ nav: # Entities via attributes -Since Shopware v6.6.3.0, it has been possible to register entities via PHP attributes. This guide will demonstrate the process. +Since Shopware v6.6.3.0, it has been possible to register entities via PHP attributes. +This guide will demonstrate the process. ## Define the entity -First, you need to define your entity. This is done by creating a new class extending `Entity` and adding the `Entity` attribute to it. The `name` parameter denotes the name of the entity. It is required and must be unique. +First, you need to define your entity. +This is done by creating a new class extending `Entity` and adding the `Entity` attribute to it. +The `name` parameter denotes the name of the entity. It is required and must be unique. -You can also supply the entity collection class to use for this entity, by specifying the `collectionClass` parameter. The default `EntityCollection` class is used if none is specified. +You can also supply the entity collection class to use for this entity, by specifying the `collectionClass` parameter. +The default `EntityCollection` class is used if none is specified. +Note: this is only possible since 6.6.9.0 -You have to define a primary key. The primary key is defined by adding the `PrimaryKey` attribute to a property. In theory, the primary key can be of any type, but it is recommended to use a `UUID`. +You have to define a primary key. The primary key is defined by adding the `PrimaryKey` attribute to a property. +In theory, the primary key can be of any type, but it is recommended to use a `UUID`. ```php - @@ -46,13 +55,26 @@ To register the entity, you have to add this class to the DI container in the `s ``` -That's it. Your entity is registered and you can read and write data to it over the DAL. Using the tag, Shopware automatically registers an `EntityDefinition` and `EntityRepository` for the entity. +That's it. +Your entity is registered, and you can read and write data to it over the DAL. +Using the tag, Shopware automatically registers an `EntityDefinition` and `EntityRepository` for the entity. ## Field Types -To define more fields, you typically use the `Field` attribute. The `Field` attribute requires the `type` parameter, which is the type of the field. The type can be any of the `FieldType` constants. +To define more fields, you typically use the `Field` attribute. +The `Field` attribute requires the `type` parameter, which is the type of the field. +The type can be any of the `FieldType` constants. ```php +|null */ @@ -434,6 +589,12 @@ class ExampleEntity extends Entity #[ManyToMany(entity: 'currency', onDelete: OnDelete::CASCADE)] public ?array $currencies = null; + /** + * @var array + */ + #[ManyToMany(entity: 'order', onDelete: OnDelete::CASCADE)] + public ?array $orders = null; + /** * @var array|null */ @@ -441,4 +602,23 @@ class ExampleEntity extends Entity public ?array $translations = null; } + + + */ +class ExampleEntityCollection extends EntityCollection +{ + protected function getExpectedClass(): string + { + return ExampleEntity::class; + } +} ```