/** * Sets image mapping for variants * * @param array $relationGroups * @param int $imageId */ protected function setImageMappings($relationGroups, $imageId) { /** @var Repository $articleRepository */ $articleRepository = $this->manager->getRepository(Article::class); $query = $articleRepository->getArticleImageDataQuery($imageId); $image = $query->getOneOrNullResult(AbstractQuery::HYDRATE_OBJECT); $imageData = $query->getOneOrNullResult(AbstractQuery::HYDRATE_ARRAY); foreach ($relationGroups as $relationGroup) { $optionCollection = []; $mapping = new Image\Mapping(); foreach ($relationGroup as $relation) { $optionModel = $relation['option']; $optionCollection[] = $optionModel; $rule = new Image\Rule(); $rule->setMapping($mapping); $rule->setOption($optionModel); $mapping->getRules()->add($rule); $mapping->setImage($image); $this->manager->persist($mapping); } $this->createImagesForOptions($optionCollection, $imageData, $image); $this->manager->flush(); } }
/** * @param Collection $options * @param Image $image * @return Image\Mapping */ public function createImageMappingForOptions(Collection $options, Image $image) { $mapping = new Image\Mapping(); $mapping->setImage($image); foreach ($options as $option) { $rule = new Image\Rule(); $rule->setMapping($mapping); $rule->setOption($option); $mapping->getRules()->add($rule); } $image->getMappings()->add($mapping); return $mapping; }