# Zusatzfelder per Plugin Migration Script anlegen

**URL:** https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303
**Category:** Programmierung
**Created:** [28. Januar 2020 um 13:51 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303 "2020-01-28T13:51:30Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Exe](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@Exe](https://forum.shopware.com/u/Exe)
#### Post date: [28. Januar 2020 um 13:51 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/1 "2020-01-28T13:51:30Z")

</div>

Hi, ich hab ich der Doku nichts gefunden, wie ich beio der Plugininstallation mit dem Migrations-Script Zusatzfelder für Produkte (Set mit Feldern) anlegen kann.

Kennt jemand einen Weg, wie das geht?

---

<div class="post-metadata">

### Author: ![pino](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/pino/32/14373_2.png) [@pino](https://forum.shopware.com/u/pino)
#### Post date: [29. Januar 2020 um 09:52 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/2 "2020-01-29T09:52:34Z")

</div>

nimm dazu&nbsp;

```
$this->container->get('custom_field_set.repository')

```

beispiel ungetestet:  
&nbsp;

```
// dein Plugin Bootstrap
public function install(InstallContext $context): void {
    $customFields = [
        [
            'name' => 'my_custom_field_set',
            'active' => true,
            'config' => [
                'label' => [
                    'en-GB' => 'my customFieldSet label',
                    'de-DE' => 'mein customFieldSet label'
                ],
            ],
            'customFields' => [
                [
                    'name' => 'my_custom_field_config',
                    'type' => \Shopware\Core\System\CustomField\CustomFieldTypes::TEXT,
                    'config' => [
                        'label' => [
                            'en-GB' => 'my customFieldConfig label',
                            'de-DE' => 'mein customFieldConfig label',
                        ]
                    ]
                ],
                [
                    'name' => 'my_custom_field_other',
                    'type' => \Shopware\Core\System\CustomField\CustomFieldTypes::TEXT,
                    'other' => [
                        'label' => [
                            'en-GB' => 'my customFieldOther label',
                            'de-DE' => 'mein customFieldOther label',
                        ]
                    ]
                ]
            ],
            'relations' => [
                [
                    'entityName' => 'product'
                ]
            ]
        ]
    ];
    $repo = $this->container->get('custom_field_set.repository');

    foreach ($customFields as $customFieldSet) {
        $repo->upsert([$customFieldSet], $context);
    }
}

```

&nbsp;

---

<div class="post-metadata">

### Author: ![Exe](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@Exe](https://forum.shopware.com/u/Exe)
#### Post date: [29. Januar 2020 um 10:18 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/3 "2020-01-29T10:18:11Z")

</div>

Oh sieht gut aus. Muss ich nachher mal testen. Danke

---

<div class="post-metadata">

### Author: ![doerndorfer](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@doerndorfer](https://forum.shopware.com/u/doerndorfer)
#### Post date: [30. Januar 2020 um 17:21 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/4 "2020-01-30T17:21:53Z")

</div>

Hi,

ich habe die Diskussion hier verfolgt und habe gerade ein ähnliches Problem. Ich versuche für die Eigenschaften ein Zusatzfeld anzulegen (also property\_group\_option).&nbsp;

Die CustomFields an sich werden über das Migrations-Script (siehe oben) angelegt und sind auch in der DB vorhanden.

```
$customFieldSet = [
    [
        'name' => 'custom_option',
        'active' => true,
        'config' => [
            'label' => [
                'en-GB' => 'Custom Fields Configurator',
                'de-DE' => 'Zusatzfelder Konfigurator'
            ],
        ],
        'customFields' => [
            [
                'name' => 'custom_option_price',
                'type' => CustomFieldTypes::TEXT,
                'config' => [
                    'label' => [
                        'en-GB' => 'Price',
                        'de-DE' => 'Preis',
                    ]
                ]
            ],
        ],
        'relations' => [
            [
                'entityName' => 'option'
            ]
        ]
    ]
];
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create($customFieldSet, Context::createDefaultContext());

```

Die Frage ist nun, wie bekomme ich das Feld in der entsprechenden Maske bei den Eigenschaften angezeigt. Die Anleitung hier ([Shopware 6: Add field to module in administration](https://docs.shopware.com/en/shopware-platform-dev-en/how-to/add-admin-new-field)) bin ich schon durch.&nbsp;

Im Template&nbsp;sw-property-option-detail.html.twig habe ich das Feld versucht so anzulegen

&nbsp;

---

<div class="post-metadata">

### Author: ![Exe](https://avatars.discourse-cdn.com/v4/letter/e/49beb7/32.png) [@Exe](https://forum.shopware.com/u/Exe)
#### Post date: [6. Februar 2020 um 07:53 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/5 "2020-02-06T07:53:29Z")

</div>

Ich würde sagen, die Zuordnung “option” gibt es nicht.

```
'relations' => [
            [
                'entityName' => 'option'
            ]
        ]

```

Ich habe zb “product” und das Feld wird dann auch automatisch bei der Bearbeitung von Produkten mit angezeigt.

Ich habe übrigens mir die Installation von SwagSocialShopping&nbsp;angeschaut und den Installer für mein Plugin nachgebaut. Die haben noch ein paar Checks eingebaut, dass bei einer Neuinstalltion nicht erneut versucht wird, die Felder anzulegen.

---

<div class="post-metadata">

### Author: ![pino](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/pino/32/14373_2.png) [@pino](https://forum.shopware.com/u/pino)
#### Post date: [12. März 2020 um 13:39 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/6 "2020-03-12T13:39:24Z")

</div>

ich führe nur ein&nbsp;upsert aus.

```
$this->customFieldSetRepository->upsert(...);

```

&nbsp;

---

<div class="post-metadata">

### Author: ![vx2020](https://avatars.discourse-cdn.com/v4/letter/v/9d8465/32.png) [@vx2020](https://forum.shopware.com/u/vx2020)
#### Post date: [1. April 2020 um 08:27 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/7 "2020-04-01T08:27:44Z")

</div>

Hallo zusammen,

ich habe ein ähnliches oder vielleicht sogar das gleich Problem wie&nbsp;doerndorfer.

Allerdings habe ich bei bestehenden PropertyValues ein Zusatzfeld, welches mit den den customFields-Einträgen aus der Datenbank kommuniziert.

Ich habe das Problem, dass ich nun keine neuen PropertyValues anlegen kann, weil es dieses Feld angeblch nicht gibt.&nbsp;&nbsp;

![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/3/331723e0faef93d8946954abd3466e1a428a78cd.png)

Das ist die fehlermeldung dazu.

Hat dafür jemand eine Lösung?

@doerndorfer‍ der entityName lautet ‚property\_group\_option‘ nicht ‚option‘

---

<div class="post-metadata">

### Author: ![lhairman](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lhairman](https://forum.shopware.com/u/lhairman)
#### Post date: [30. November 2020 um 09:36 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/8 "2020-11-30T09:36:55Z")

</div>

> [@pino schrieb:](https://forum.shopware.com/profile/1734/pino "pino")
> 
> ich führe nur ein&nbsp;upsert aus.
> 
> $this-\>customFieldSetRepository-\>upsert(…);
> 
> &nbsp;

Hallo!

Für ein Plugin lege ich&nbsp;

```
public function install(InstallContext $context): void

```

&nbsp;meine Zusatzfelder an. Das klappt hervorragend, so wie es hier beschrieben wurde.&nbsp;

Beim Deinstallieren möchte ich die Felder nicht löschen.

Wenn nun das Plugin wieder installiert werden soll, so&nbsp;ist dies nicht möglich, da die Zusatzfelder bereits existieren. Und das obwohl ich ein „upsert“ aufrufe.

Kann mir da jemand weiterhelfen?&nbsp;

Danke!

---

<div class="post-metadata">

### Author: ![xlarry](https://avatars.discourse-cdn.com/v4/letter/x/51bf81/32.png) [@xlarry](https://forum.shopware.com/u/xlarry)
#### Post date: [20. Dezember 2020 um 11:41 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/9 "2020-12-20T11:41:37Z")

</div>

Hallo @lhairman‍

das scheint bei mir auch so zu sein. Obwohl ich vermutet hätte, dass genau dieser Fehler bei einem `upsert()` nicht auftritt.

Ich führe nun vor dem `upsert()` ein `search()` durch, um zu prüfen, ob das Custom Field Set bereits existiert:

```
$criteria = (new Criteria())
	->addFilter( new EqualsFilter('name', $customFieldSetName) );

$existingCustomFieldSets = $customFieldSetRepository
	->search($criteria, $context->getContext())
	->getTotal();
	
if ($existingCustomFieldSets == 0) {
	$customFieldSetRepository->upsert($customFieldSet, $context->getContext());
}

```

Lg

---

<div class="post-metadata">

### Author: ![lhairman](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lhairman](https://forum.shopware.com/u/lhairman)
#### Post date: [20. Dezember 2020 um 16:03 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/10 "2020-12-20T16:03:20Z")

</div>

Danke für die Info! Ich habe bis jetzt auch noch keine andere Lösung gefunden. Werde ich mit dem search probieren.&nbsp;

---

<div class="post-metadata">

### Author: ![moonshot](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/moonshot/32/24894_2.png) [@moonshot](https://forum.shopware.com/u/moonshot)
#### Post date: [23. Oktober 2024 um 17:04 UTC](https://forum.shopware.com/t/zusatzfelder-per-plugin-migration-script-anlegen/64303/11 "2024-10-23T17:04:29Z")

</div>

Ich weiß der Thread ist schon älter, aber bin selbst auch gerade erst wieder über die Thematik gestolpert.

Dadurch, dass ich immer wieder mal neue Custom fields hinzugefügt habe, hat der Check wie von @xlarry beschrieben nicht mehr ausgereicht.

Deswegen habe ich das ganze noch etwas erweitert und bin bei folgender Lösung gelandet (Shopware 6.6):

```php
<?php

declare(strict_types=1);

namespace XyTheme;

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
use Shopware\Core\System\CustomField\CustomFieldEntity;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Storefront\Framework\ThemeInterface;

class XyTheme extends Plugin implements ThemeInterface
{
    public function activate(Plugin\Context\ActivateContext $activateContext): void
    {
        /** @var EntityRepository<CustomFieldSetEntity> $customFieldSetRepository */
        $customFieldSetRepository = $this->container->get('custom_field_set.repository');
        /** @var EntityRepository<CustomFieldEntity> $customFieldRepository */
        $customFieldRepository = $this->container->get('custom_field.repository');
        $context = $activateContext->getContext();

        $customFields = [
            [
                'name' => 'contact_info',
                'active' => true,
                'config' => [
                    'label' => [
                        'en-GB' => 'Contact information',
                        'de-DE' => 'Kontaktinformationen'
                    ],
                ],
                'customFields' => [
                    [
                        'name' => 'email_address',
                        'type' => CustomFieldTypes::TEXT,
                        'config' => [
                            'label' => [
                                'en-GB' => 'Mail address',
                                'de-DE' => 'Mail Adresse',
                            ]
                        ]
                    ],
                    [
                        'name' => 'phone_number',
                        'type' => CustomFieldTypes::TEXT,
                        'config' => [
                            'label' => [
                                'en-GB' => 'Phone number',
                                'de-DE' => 'Telefonnummer',
                            ]
                        ]
                    ],
                    [
                        'name' => 'opening_hours',
                        'type' => CustomFieldTypes::TEXT,
                        'config' => [
                            'label' => [
                                'en-GB' => 'Opening hours',
                                'de-DE' => 'Öffnungszeiten',
                            ]
                        ]
                    ],
                ],
                'relations' => [
                    [
                        'entityName' => 'sales_channel'
                    ]
                ]
            ]
        ];

        foreach ($customFields as $customFieldSet) {
            $criteria = (new Criteria())->addFilter(new EqualsFilter('name', $customFieldSet['name']));
            $customFieldSetId = $customFieldSetRepository
                ->search($criteria, $context)
                ->first()
                ?->getUniqueIdentifier();

            if ($customFieldSetId === null) {
                $customFieldSetRepository->upsert([$customFieldSet], $context);
            } else {
                foreach ($customFieldSet['customFields'] as $customField) {
                    $criteria = (new Criteria())
                        ->addFilter(new EqualsFilter('customFieldSetId', $customFieldSetId))
                        ->addFilter(new EqualsFilter('name', $customField['name']));

                    $customFieldId = $customFieldRepository
                        ->search($criteria, $context)
                        ->first()
                        ?->getUniqueIdentifier();

                    if ($customFieldId !== null) {
                        $customField['id'] = $customFieldId;
                    }

                    $customField['customFieldSetId'] = $customFieldSetId;
                    $customFieldRepository->upsert([$customField], $context);
                }
            }
        }
    }
}

```
