# Direkt Zuweisung Kunde zu Produkt

**URL:** https://forum.shopware.com/t/direkt-zuweisung-kunde-zu-produkt/71042
**Category:** Programmierung
**Created:** [19. November 2020 um 18:34 UTC](https://forum.shopware.com/t/direkt-zuweisung-kunde-zu-produkt/71042 "2020-11-19T18:34:28Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Moorleiche](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Moorleiche](https://forum.shopware.com/u/Moorleiche)
#### Post date: [19. November 2020 um 18:34 UTC](https://forum.shopware.com/t/direkt-zuweisung-kunde-zu-produkt/71042/1 "2020-11-19T18:34:28Z")

</div>

Moin,

wollte mal Fragen ob jmd sowas schon gebaut hat. Ich benötige eine ManyToMany Assoziation Kunde zu Produkt und bestenfalls sollte es eine neue Kategorie für Kunden mit dieser speziellen Assoziation geben. In dieser Kategorie sollen via Produkt-Stream auch nur diese Produkte geladen werden, welche die Direktzuweisung zum eingeloggten Kunden haben.

Ich habe selbst schon was vorbereitet aber irgendwie habe ich das Gefühl dass ManyToMany Assoziationen von zwei Core Entities via Plugin nicht möglich sind?!

VG

---

<div class="post-metadata">

### Author: ![Moorleiche](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Moorleiche](https://forum.shopware.com/u/Moorleiche)
#### Post date: [25. November 2020 um 11:53 UTC](https://forum.shopware.com/t/direkt-zuweisung-kunde-zu-produkt/71042/2 "2020-11-25T11:53:58Z")

</div>

Also nochmal zur Anmekung: Diesen Fehler bekomme ich nur wenn ich versuche eine ManyToMany Assoziation zwischen zwei Core Entities aufzubauen. Selbige Assoziation zwischen Core und Custom Entity geht soweit…&nbsp;

```
class ProductCustomerDefinition extends MappingEntityDefinition
{
    public const ENTITY_NAME = 'moorl_product_customer';

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

    protected function defineFields(): FieldCollection
    {
        return new FieldCollection([
            (new FkField('product_id', 'productId', ProductDefinition::class))->addFlags(new PrimaryKey(), new Required()),
            (new ReferenceVersionField(ProductDefinition::class))->addFlags(new PrimaryKey(), new Required()),
            (new FkField('customer_id', 'customerId', CustomerDefinition::class))->addFlags(new PrimaryKey(), new Required()),
            new ManyToOneAssociationField('product', 'product_id', ProductDefinition::class),
            new ManyToOneAssociationField('customer', 'customer_id', CustomerDefinition::class)
        ]);
    }
}

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

    public function extendFields(FieldCollection $collection): void
    {
        $collection->add(
            (new ManyToManyAssociationField(
                'moorlCustomers',
                CustomerDefinition::class,
                ProductCustomerDefinition::class,
                'customer_id',
                'product_id'
            ))->addFlags(new Inherited())
        );
    }
}

API Request:
/api/v3/product/ac27b9bf1e724b839d16eb41b7a8f531/extensions/moorlCustomers

{"id":"5985f020dd0d48fd9fbe16890b41fc03"}

API Response:

```

 ![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/1/115739a8efca9f70894db8ca310136de99754e18.png)

---

<div class="post-metadata">

### Author: ![Moorleiche](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Moorleiche](https://forum.shopware.com/u/Moorleiche)
#### Post date: [25. November 2020 um 12:01 UTC](https://forum.shopware.com/t/direkt-zuweisung-kunde-zu-produkt/71042/3 "2020-11-25T12:01:53Z")

</div>

Wenn ich für customerId den Required-Flag entferne, dann bekomme ich diesen Fehler:

```
detail: "An exception occurred while executing 'REPLACE INTO moorl_product_customer (product_id, product_version_id, created_at) VALUES ('Y�� �\rH����A�','���jK¾K��u,4%','2020-11-25 11:59:28.189');':↵↵SQLSTATE[HY000]: General error: 1364 Field 'customer_id' doesn't have a default value"

```

Aber die&nbsp;customerId ist ja wie im vorherigen Bsp im Request Payload enthalten. Also wieso die diese nicht in die Datenbank geschrieben?

---

<div class="post-metadata">

### Author: ![shopwarer1638](https://avatars.discourse-cdn.com/v4/letter/s/76d3ee/32.png) [@shopwarer1638](https://forum.shopware.com/u/shopwarer1638)
#### Post date: [13. Juli 2023 um 16:53 UTC](https://forum.shopware.com/t/direkt-zuweisung-kunde-zu-produkt/71042/4 "2023-07-13T16:53:56Z")

</div>

@Moorleiche Haben Sie die Ursache für den ‚General error: 1364 Field‘? Ich habe den gleichen Fehler, wenn ich meine Migration einrichte
