/**
  * {@inheritDoc}
  */
 public function setManyToOne($data, $model, $property)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setManyToOne', array($data, $model, $property));
     return parent::setManyToOne($data, $model, $property);
 }
示例#2
0
 /**
  * @param array $data
  * @param \Shopware\Models\Article\Article $article
  * @throws \Shopware\Components\Api\Exception\CustomValidationException
  * @throws \Exception
  * @return array
  */
 protected function prepareConfiguratorSet($data, ArticleModel $article)
 {
     if (!isset($data['configuratorSet'])) {
         return $data;
     }
     $configuratorSet = $article->getConfiguratorSet();
     if (!$configuratorSet) {
         $configuratorSet = new Configurator\Set();
         if (isset($data['mainDetail']['number'])) {
             $number = $data['mainDetail']['number'];
         } else {
             $number = $article->getMainDetail()->getNumber();
         }
         $configuratorSet->setName('Set-' . $number);
         $configuratorSet->setPublic(false);
     }
     if (isset($data['configuratorSet']['type'])) {
         $configuratorSet->setType($data['configuratorSet']['type']);
     }
     if (isset($data['configuratorSet']['name'])) {
         $configuratorSet->setName($data['configuratorSet']['name']);
     }
     $allOptions = [];
     $allGroups = [];
     $groupPosition = 0;
     foreach ($data['configuratorSet']['groups'] as $groupData) {
         $group = null;
         if (isset($groupData['id'])) {
             $group = $this->getManager()->getRepository('Shopware\\Models\\Article\\Configurator\\Group')->find($groupData['id']);
             if (!$group) {
                 throw new ApiException\CustomValidationException(sprintf("ConfiguratorGroup by id %s not found", $groupData['id']));
             }
         } elseif (isset($groupData['name'])) {
             $group = $this->getManager()->getRepository('Shopware\\Models\\Article\\Configurator\\Group')->findOneBy(array('name' => $groupData['name']));
             if (!$group) {
                 $group = new Configurator\Group();
                 $group->setPosition($groupPosition);
             }
         } else {
             throw new ApiException\CustomValidationException('At least the groupname is required');
         }
         $groupOptions = [];
         $optionPosition = 0;
         foreach ($groupData['options'] as $optionData) {
             $option = null;
             if ($group->getId() > 0) {
                 if (isset($optionData['id'])) {
                     $option = $this->getManager()->find('Shopware\\Models\\Article\\Configurator\\Option', $optionData['id']);
                     if (!$option) {
                         throw new ApiException\CustomValidationException(sprintf("ConfiguratorOption by id %s not found", $optionData['id']));
                     }
                 } else {
                     $option = $this->getManager()->getRepository('Shopware\\Models\\Article\\Configurator\\Option')->findOneBy(['name' => $optionData['name'], 'groupId' => $group->getId()]);
                 }
             }
             if (!$option) {
                 $option = new Configurator\Option();
             }
             $option->fromArray($optionData);
             $option->setGroup($group);
             if (!isset($optionData['position'])) {
                 $option->setPosition($optionPosition++);
             }
             $allOptions[] = $option;
             $groupOptions[] = $option;
         }
         $groupData['options'] = $groupOptions;
         $group->fromArray($groupData);
         $allGroups[] = $group;
     }
     // Clear needed in order to allow updates on configuratorSet. When removed constraints in
     // s_article_configurator_set_group_relations and s_article_configurator_set_option_relations
     // might fail.
     $configuratorSet->getOptions()->clear();
     $configuratorSet->setOptions($allOptions);
     $configuratorSet->getGroups()->clear();
     $configuratorSet->setGroups($allGroups);
     $this->getManager()->persist($configuratorSet);
     $data['configuratorSet'] = $configuratorSet;
     return $data;
 }