# Durchsuchbares Produkt Repository für Zusatzfeld Objekt Produkt

**URL:** <https://forum.shopware.com/t/durchsuchbares-produkt-repository-fuer-zusatzfeld-objekt-produkt/99314>\
**Category:** Programmierung\
**Created:** [13. April 2023 um 08:50 UTC](https://forum.shopware.com/t/durchsuchbares-produkt-repository-fuer-zusatzfeld-objekt-produkt/99314 "2023-04-13T08:50:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ergobasis](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@ergobasis](https://forum.shopware.com/u/ergobasis)\
**Post date:** [13. April 2023 um 08:50 UTC](https://forum.shopware.com/t/durchsuchbares-produkt-repository-fuer-zusatzfeld-objekt-produkt/99314/1 "2023-04-13T08:50:56Z")

</div>

Ich versuche mich gerade an einem Plugin, dass mir ein durchsuchbares Produkt Repository für meinen aktuellen Saleschannel bereitstellt. Darüber will ich an die Produkt-Objekte herankommen.  
Ich bekomme jetzt aber, egal wie ich meine Klasse nenne, einen Error.  
Ich hoffe mir kann jemand sagen, was mein Fehler ist, ich bin am Verzweifeln.

```auto
Compile Error: Cannot declare class Roth\ProductRepo\Twig\RothProductRepo, because the name is already in use

```

Mein Plugin ist wie folgt aufgebaut

```auto
RothProductRepo
-composer.json
-src
--Resources
---config
----services.xml
--Twig
---RothProductRepository.php
--RothProductRepo.php

```

composer.json

```auto
{
  "name": "roth/product-repo",
  "description": "stellt ein durchsuchbares Produkt Repository bereit",
  "version": "1.0.0",
  "type": "shopware-platform-plugin",
  "license": "MIT",
  "authors": [
        {
            "name": "Konstantin Roth"
        }
    ],
  "autoload": {
    "psr-4": {
      "Roth\\ProductRepo\\": "src/"
    }
  },
  "extra": {
    "shopware-plugin-class": "Roth\\ProductRepo\\RothProductRepository",
    "label": {
      "de-DE": "durchsuchbares Produkt Repository",
      "en-GB": "searchable Product Repository"
    }
  }
}

```

RothProductRepo.php

```auto
<?php declare(strict_types=1);

namespace Roth\ProductRepo;

use Shopware\Core\Framework\Plugin;

class RothProductRepo extends Plugin
{
}

```

RothProductRepository.php

```auto
<?php declare(strict_types=1);

namespace Roth\ProductRepo\Twig;

use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class RothProductRepo
{

  /**
   * @var SalesChannelRepositoryInterface
   */
  private $salesChannelProductRepository;

  /*
  * Construct
  */
  public function __construct(SalesChannelRepositoryInterface $salesChannelProductRepository)
  {
    $this->salesChannelProductRepository = $salesChannelProductRepository;
  }

  public function searchProductByID($productID, $context)
  {
    return
      $this->salesChannelProductRepository->search(new Criteria([$productID]), $context)->first();
  }
}

```

services.xml

```auto
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
	<!-- SERVICES-->
		<service id="Roth\ProductRepo\Twig\RothProductRepository" public="true">
            <tag name="twig.extension"/> <!--Required-->
			<argument type="service" id="sales_channel.product.repository"/>
        </service>
    </services>
</container>

```

---

<div class="post-metadata">

**Author:** ![Max\_Shop](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Max\_Shop](https://forum.shopware.com/u/Max_Shop)\
**Post date:** [13. April 2023 um 10:56 UTC](https://forum.shopware.com/t/durchsuchbares-produkt-repository-fuer-zusatzfeld-objekt-produkt/99314/2 "2023-04-13T10:56:05Z")

</div>

Müssen Filename und Classname nicht immer übereinstimmen?

---

<div class="post-metadata">

**Author:** ![ergobasis](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@ergobasis](https://forum.shopware.com/u/ergobasis)\
**Post date:** [13. April 2023 um 11:16 UTC](https://forum.shopware.com/t/durchsuchbares-produkt-repository-fuer-zusatzfeld-objekt-produkt/99314/3 "2023-04-13T11:16:07Z")

</div>

Habs gerade probiert, es könnte stimmen, allerdings bekomme ich immer noch denselben Fehler…

---

<div class="post-metadata">

**Author:** ![AlexGalax](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/alexgalax/32/9925_2.png) [@AlexGalax](https://forum.shopware.com/u/AlexGalax)\
**Post date:** [13. April 2023 um 13:25 UTC](https://forum.shopware.com/t/durchsuchbares-produkt-repository-fuer-zusatzfeld-objekt-produkt/99314/4 "2023-04-13T13:25:03Z")

</div>

Es steht doch in der Fehlermeldung - `RothProductRepo` wird als Klassenname mehrfach verwendet.  
In der `RothProductRepo.php` und in der `RothProductRepository.php`. Die zweite Klasse müsste `RothProductRepository` heißen. Mit dem Klassennamen lädst du sie ja auch in der XML-Datei.

Außerdem sollte die `shopware-plugin-class` die Plugin-Instanz initialisieren: `Roth\\ProductRepo\\RothProductRepo`

---

<div class="post-metadata">

**Author:** ![ergobasis](https://avatars.discourse-cdn.com/v4/letter/e/439d5e/32.png) [@ergobasis](https://forum.shopware.com/u/ergobasis)\
**Post date:** [17. April 2023 um 07:52 UTC](https://forum.shopware.com/t/durchsuchbares-produkt-repository-fuer-zusatzfeld-objekt-produkt/99314/5 "2023-04-17T07:52:35Z")

</div>

Ich hab es hinbekommen.  
So schaut das ganz Final aus, für alle die auch ein Produkt Repositiory bereitstellen wollen, das mit der Produkt ID durchsucht werden kann.

Übersicht

```auto
RothProductRepo
-composer.json
-src
--Resources
---config
----services.xml
--Twig
---RothProductRepository.php
--RothProductRepo.php

```

composer.json

```auto
{
  "name": "roth/product-repo",
  "description": "stellt ein durchsuchbares Produkt Repository bereit",
  "version": "1.0.0",
  "type": "shopware-platform-plugin",
  "license": "MIT",
  "authors": [
        {
            "name": "Konstantin Roth"
        }
    ],
  "autoload": {
    "psr-4": {
      "Roth\\ProductRepo\\": "src/"
    }
  },
  "extra": {
    "shopware-plugin-class": "Roth\\ProductRepo\\RothProductRepo",
    "label": {
      "de-DE": "durchsuchbares Produkt Repository",
      "en-GB": "searchable Product Repository"
    }
  }
}

```

RothProductRepo.php

```auto
<?php declare(strict_types=1);

namespace Roth\ProductRepo;

use Shopware\Core\Framework\Plugin;

class RothProductRepo extends Plugin
{
}

```

RothProductRepository.php

```auto
<?php declare(strict_types=1);

namespace Roth\ProductRepo\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;

class RothProductRepository extends AbstractExtension
{

  /**
   * @var SalesChannelRepositoryInterface
   */
  private $salesChannelProductRepository;

  /*
  * Construct
  */
  public function __construct(SalesChannelRepositoryInterface $salesChannelProductRepository)
  {
    $this->salesChannelProductRepository = $salesChannelProductRepository;
  }

  public function getFunctions()
  {
    return [
      new TwigFunction('getProductByID', [$this, 'searchProductByID']),
    ];
  }

  public function searchProductByID($productID, $context)
  {
    return
      $this->salesChannelProductRepository->search(new Criteria([$productID]), $context)->first();
  }
}

```

services.xml

```auto
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
	<!-- SERVICES-->
		<service id="Roth\ProductRepo\Twig\RothProductRepository" public="true">
            <tag name="twig.extension"/> <!--Required-->
			<argument type="service" id="sales_channel.product.repository"/>
        </service>
    </services>
</container>

```
