Anlegen eines ConfiguratorSets via Models

Guten Morgen zusammen,

Ich möchte gerne ein Configuratorset über die Models anlegen. Das funktioniert ganz gut, einzig die Verknüpfung zwischen Set und options (s_article_configurator_set_option_relations) und Set und Groups (s_article_configurator_set_group_relations) funktioniert leider nicht.

// Create configurator Set if it does not exist
if (!$configuratorSet = $this->em->getRepository(Set::class)->findOneBy(array('name' => $this->configuratorSetName))):
    $configuratorSet = new Set();
    $configuratorSet->setName($this->configuratorSetName);
endif;

// Create configurator Group if it does not exist
if(!$configuratorGroup = $this->em->getRepository(Group::class)->findOneBy(array('name' => $this->configuratorGroupName))):
    $configuratorGroup = new Group();
    $configuratorGroup->setName($this->configuratorGroupName);
    $configuratorGroup->setPosition(0);
endif;
$this->em->persist($configuratorGroup);
$groupCollection = new ArrayCollection();
$groupCollection->add($configuratorGroup);

// Create configurator options
$configuratorOption = new Option();
$configuratorOption->setName($this->getPackageSizeName($csv[3], $csv[4]));
$configuratorOption->setGroup($configuratorGroup);
$configuratorOption->setPosition(0);
$this->em->persist($configuratorOption);

$optionCollection = new ArrayCollection();
$optionCollection->add($configuratorOption);

$configuratorSet->setGroups($groupCollection);
$configuratorSet->setOptions($optionCollection);
$this->em->persist($configuratorSet);

$this->em->flush();

Anschließend sind alle Daten vorhanden nur die beiden oben genannten Tabellen enthalten keine Daten. Obwohl das nach meinem Verständnis eigentlich mit setGroups() und setOptions() der Fall sein sollte.

Danke vorweg für Eure Tipps & LG

Hallo @Synonymous‍,

Ihre Frage war eine neue Herausforderung für mich.

Wie ich galube, gibt hier ein Bug in Shopware, weil in Shopware\Models\Article\Configurator\Set haben Sie $groups Variable als ArrayCollection declieration.

aber in Line 179 in setGroups($groups) gibt eine Problem.

Genau mit setOneToMany Funktion. Dieser Funktion akzeptiert $data als Array und nicht als ArrayCollection wie Sie hier können sehen:

und schauen Sie mal Line 189.

Also die Lösung für Ihr Problem ist:

// Create configurator Set if it does not exist
if (!$configuratorSet = $this->em->getRepository(Set::class)->findOneBy(array('name' => $this->configuratorSetName))):
    $configuratorSet = new Set();
    $configuratorSet->setName($this->configuratorSetName);
endif;

// Create configurator Group if it does not exist
if(!$configuratorGroup = $this->em->getRepository(Group::class)->findOneBy(array('name' => $this->configuratorGroupName))):
    $configuratorGroup = new Group();
    $configuratorGroup->setName($this->configuratorGroupName);
    $configuratorGroup->setPosition(0);
endif;
//brauchen Sie diese Line nicht, weil sowieso ConfiguratorSet werde diese machen
//$this->em->persist($configuratorGroup);
$groupCollection = new ArrayCollection();
$groupCollection->add($configuratorGroup);

// Create configurator options
$configuratorOption = new Option();
$configuratorOption->setName($this->getPackageSizeName($csv[3], $csv[4]));
$configuratorOption->setGroup($configuratorGroup);
$configuratorOption->setPosition(0);
// S.O.
//$this->em->persist($configuratorOption);

$optionCollection = new ArrayCollection();
$optionCollection->add($configuratorOption);
//Ihre Lösung ->toArray()
$configuratorSet->setGroups($groupCollection->toArray());
$configuratorSet->setOptions($optionCollection->toArray());
$this->em->persist($configuratorSet);

$this->em->flush();

Viele Grüße,

Ahmad.

1 „Gefällt mir“