/**
  * Add products and groups to associations
  *
  * @param ProductInterface $product
  * @param mixed            $data
  */
 protected function addProductsAndGroupsToAssociations(ProductInterface $product, $data)
 {
     foreach ($data as $typeCode => $items) {
         $association = $product->getAssociationForTypeCode($typeCode);
         if (null === $association) {
             throw InvalidArgumentException::expected('associations', 'existing association type code', 'adder', 'association', $typeCode);
         }
         $this->addAssociatedProducts($association, $items['products']);
         $this->addAssociatedGroups($association, $items['groups']);
     }
 }
Ejemplo n.º 2
0
 /**
  * @Given /^the following associations for the (product "([^"]+)"):$/
  */
 public function theFollowingAssociationsForTheProduct(ProductInterface $owner, $id, TableNode $values)
 {
     $rows = $values->getHash();
     foreach ($rows as $row) {
         $association = $owner->getAssociationForTypeCode($row['type']);
         if (null === $association) {
             $associationType = $this->getContainer()->get('pim_catalog.repository.association_type')->findOneBy(['code' => $row['type']]);
             $association = new Association();
             $association->setAssociationType($associationType);
             $owner->addAssociation($association);
         }
         $association->addProduct($this->getProduct($row['product']));
     }
     $this->getProductSaver()->save($owner, ['recalculate' => false]);
 }
 /**
  * Denormalize product associations
  *
  * @param string           &$data
  * @param string           $format
  * @param array            $context
  * @param ProductInterface $product
  *
  * @throws \RuntimeException
  */
 protected function denormalizeAssociations(&$data, $format, array $context, ProductInterface $product)
 {
     foreach ($product->getAssociations() as $association) {
         foreach ($association->getGroups() as $group) {
             $association->removeGroup($group);
         }
         foreach ($association->getProducts() as $prod) {
             $association->removeProduct($prod);
         }
     }
     // Get association field names and add associations
     $assocFieldNames = $this->assocFieldResolver->resolveAssociationColumns();
     foreach ($assocFieldNames as $assocFieldName) {
         if (isset($data[$assocFieldName])) {
             if (strlen($data[$assocFieldName]) > 0) {
                 list($associationTypeCode, $part) = explode('-', $assocFieldName);
                 $association = $product->getAssociationForTypeCode($associationTypeCode);
                 $association = $this->serializer->denormalize($data[$assocFieldName], $this->associationClass, $format, ['entity' => $association, 'association_type_code' => $associationTypeCode, 'part' => $part] + $context);
                 if (!$product->getAssociationForTypeCode($associationTypeCode)) {
                     $product->addAssociation($association);
                 }
             }
             unset($data[$assocFieldName]);
         }
     }
     foreach (array_keys($data) as $fieldName) {
         if (null !== ($matches = $this->extractAssociationFieldNameInfos($fieldName))) {
             throw new \RuntimeException(sprintf('Association type "%s" does not exist anymore', $matches['assoc_type_code']));
         }
     }
 }