# Umgang mit version\_id in eigenen Extensions

**URL:** <https://forum.shopware.com/t/umgang-mit-version-id-in-eigenen-extensions/106997>\
**Category:** Shopware 6 (German)\
**Created:** [7. April 2025 um 13:51 UTC](https://forum.shopware.com/t/umgang-mit-version-id-in-eigenen-extensions/106997 "2025-04-07T13:51:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NikolajRuppert](https://avatars.discourse-cdn.com/v4/letter/n/7ab992/32.png) [@NikolajRuppert](https://forum.shopware.com/u/NikolajRuppert)\
**Post date:** [7. April 2025 um 13:51 UTC](https://forum.shopware.com/t/umgang-mit-version-id-in-eigenen-extensions/106997/1 "2025-04-07T13:51:41Z")

</div>

Hallo liebe Community,

in meinem Plugin versuche ich das Datenmodel so zu erweitern, dass ich einem Produkt Kundengruppen (Mehrzahl!) zuweisen kann. Die Erweiterung sieht, bis jetzt so aus:  
Neue Entität:  
Collection:

```auto
class ProductCustomerGroupCollection extends EntityCollection
{
    protected function getExpectedClass(): string
    {
        return ProductCustomerGroupEntity::class;
    }
}

```

Definition:

```auto
class ProductCustomerGroupDefinition extends EntityDefinition
{
    public const ENTITY_NAME = 'ust_product_customer_group';

    public function getEntityName(): string
    {
        return self::ENTITY_NAME;
    }

    public function getCollectionClass(): string
    {
        return ProductCustomerGroupCollection::class;
    }

    public function getEntityClass(): string
    {
        return ProductCustomerGroupEntity::class;
    }

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new FkField('product_id', 'productId', ProductDefinition::class))->addFlags(new Required(), new PrimaryKey()),
            (new FkField('customer_group_id', 'customerGroupId', CustomerGroupDefinition::class))->addFlags(new Required(), new PrimaryKey()),
            (new CreatedAtField())->addFlags(new ApiAware()),
            (new UpdatedAtField())->addFlags(new ApiAware()),

            new ManyToOneAssociationField('product', 'product_id', ProductDefinition::class),
            new ManyToOneAssociationField('customerGroup', 'customer_group_id', CustomerGroupDefinition::class),
        ]);
    }
}

```

und Entity:

```auto
class ProductCustomerGroupEntity extends Entity
{
    protected string $productId;
    protected string $customerGroupId;

    public function getProductId(): string
    {
        return $this->productId;
    }

    public function setProductId(string $productId): void
    {
        $this->productId = $productId;
    }

    public function getCustomerGroupId(): string
    {
        return $this->customerGroupId;
    }

    public function setCustomerGroupId(string $customerGroupId): void
    {
        $this->customerGroupId = $customerGroupId;
    }
}

```

Und dann habe ich die Product-Entity durch diese Extension erweitert:

```auto
class ProductExtension extends EntityExtension
{
    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            (new ManyToManyAssociationField(
                'customerGroups',
                CustomerGroupDefinition::class,
                ProductCustomerGroupDefinition::class,
                'product_id',
                'customer_group_id',
            ))->addFlags(new CascadeDelete())
        );
    }

    public function getDefinitionClass(): string
    {
        return ProductDefinition::class;
    }
}

```

Die Tabelle „ust\_product\_customer\_group“ wurde in einer entsprechenden Migration angelegt. Auch wurde die Administration angepasst, sodass ich nun einem Produkt Kundengruppen zuweisen kann.

Zum Problem:  
Ich versuche im Listing Produkte auf Kundengruppen einschränken, und zwar so:

```auto
public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
    {
        $criteria = $event->getCriteria();
        $context = $event->getSalesChannelContext();
        
        // Aktuelle Kundengruppe holen
        $customerGroupId = $context->getCurrentCustomerGroup()->getId();

        // Association hinzufügen
        $criteria->addAssociation('customerGroups');

        // Produkte ausschließen, die der aktuellen Kundengruppe zugewiesen sind
        $criteria->addFilter(new NotFilter(
            NotFilter::CONNECTION_AND,
            [new EqualsFilter('customerGroups.id', $customerGroupId)]
        ));
       
    }

```

Das führt zum Fehler:

```auto
An exception occurred while executing a query: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product.customerGroups.mapping.product_version_id' in 'field list'

```

Es scheint also irgendetwas mit der „version\_id“ zutun zu haben. Aber ich will meine Kundengruppen gar nicht an eine bestimmte „Version“ des Produktes hängen, sondern an das Produkt selbst. Oder verstehe ich das Konzept falsch? Wie setze ich meine Anforderung um?  
Soll ich in der Tabelle „ust\_product\_customer\_group“ die „product\_version\_id“ hinzufügen? Was ist aber, wenn die version\_id des Produktes sich ändert, dann verliere ich doch alle meine Referenzen zu Kundengruppen?  
Oder kann ich meine Criteria so definieren, dass Shopware die version\_id nicht automatisch in der Abfrage einbaut? Geht das?

Nochmal zur Klärung - die Anforderung ist einem Produkt Kundengruppen zuweisen zu können und dann, Produkte per Criteria auf bestimmte Kundengruppen im Listing (und später in der Suche) einzuschränken.

Danke im Voraus,  
Nikolaj

---

<div class="post-metadata">

**Author:** ![Anotherone](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Anotherone](https://forum.shopware.com/u/Anotherone)\
**Post date:** [7. April 2025 um 17:05 UTC](https://forum.shopware.com/t/umgang-mit-version-id-in-eigenen-extensions/106997/2 "2025-04-07T17:05:22Z")

</div>

Die version\_id wird meines Wissens bei den Produkten bis heute nicht wirklich genutzt, ist aber notwendig bei Referenzen auf product. Bisher wird das meine ich nur bei Bestellungen wirklich verwendet. Und es würde bei einer Änderung nicht zum Verlust der Zuordnung führen, da die aktuelle Version immer dieselbe feste ID (LIVE\_VERSION, 0FA91CE3E96A4BC2BE4BD9CE752C3425) hat und die alte Version eine „neue“ ID bekommt. Ich hoffe, das ist verständlich.

---

<div class="post-metadata">

**Author:** ![NikolajRuppert](https://avatars.discourse-cdn.com/v4/letter/n/7ab992/32.png) [@NikolajRuppert](https://forum.shopware.com/u/NikolajRuppert)\
**Post date:** [8. April 2025 um 09:26 UTC](https://forum.shopware.com/t/umgang-mit-version-id-in-eigenen-extensions/106997/3 "2025-04-08T09:26:45Z")

</div>

Vielen Dank Anotherone für die schnelle Antwort! Dann füge ich meiner Erweiterung noch die Productversion-Spalte hinzu und schaue wie es sich verhält..
