/**
  * {@inheritdoc}
  */
 public function findCommonAttributeIds(array $productIds)
 {
     // Prepare SQL query
     $commonAttSql = $this->prepareCommonAttributesSQLQuery();
     $commonAttSql = strtr($commonAttSql, ['%product_ids%' => '(' . implode($productIds, ',') . ')', '%product_ids_count%' => count($productIds)]);
     $commonAttSql = QueryBuilderUtility::prepareDBALQuery($this->em, $this->entityName, $commonAttSql);
     // Execute SQL query
     $stmt = $this->em->getConnection()->prepare($commonAttSql);
     $stmt->execute();
     $attributes = $stmt->fetchAll();
     $attributeIds = array();
     foreach ($attributes as $attributeId) {
         $attributeIds[] = (int) $attributeId['a_id'];
     }
     return $attributeIds;
 }
 /**
  * {@inheritdoc}
  */
 public function getEligibleProductIdsForVariantGroup($variantGroupId)
 {
     $sql = 'SELECT v.entity_id AS product_id, gp.group_id, ga.group_id ' . 'FROM pim_catalog_group g ' . 'INNER JOIN pim_catalog_group_attribute ga ON ga.group_id = g.id ' . 'INNER JOIN %product_value_table% v ON v.attribute_id = ga.attribute_id ' . 'LEFT JOIN pim_catalog_group_product gp ON (v.entity_id = gp.product_id) ' . 'INNER JOIN pim_catalog_group_type gt on gt.id = g.type_id ' . 'WHERE ga.group_id = :groupId AND gt.code = "VARIANT" ' . 'AND (v.option_id IS NOT NULL';
     if (null !== $this->referenceDataRegistry) {
         $references = $this->referenceDataRegistry->all();
         if (!empty($references)) {
             $valueMetadata = QueryBuilderUtility::getProductValueMetadata($this->_em, $this->_entityName);
             foreach ($references as $code => $referenceData) {
                 if (ConfigurationInterface::TYPE_SIMPLE === $referenceData->getType()) {
                     if ($valueMetadata->isAssociationWithSingleJoinColumn($code)) {
                         $sql .= sprintf(' OR v.%s IS NOT NULL', $valueMetadata->getSingleAssociationJoinColumnName($code));
                     }
                 }
             }
         }
     }
     $sql .= ') ' . 'GROUP BY v.entity_id ' . 'HAVING (gp.group_id IS NULL OR gp.group_id = ga.group_id) ' . 'AND COUNT(ga.attribute_id) = (SELECT COUNT(*) FROM pim_catalog_group_attribute WHERE group_id = :groupId)';
     $sql = QueryBuilderUtility::prepareDBALQuery($this->_em, $this->_entityName, $sql);
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue('groupId', $variantGroupId);
     $stmt->execute();
     $results = $stmt->fetchAll();
     $productIds = array_map(function ($row) {
         return $row['product_id'];
     }, $results);
     return $productIds;
 }
 /**
  * @param integer $variantGroupId
  *
  * @return array product ids
  */
 public function getEligibleProductIdsForVariantGroup($variantGroupId)
 {
     $sql = 'SELECT count(ga.attribute_id) as nb ' . 'FROM pim_catalog_group_attribute as ga ' . 'WHERE ga.group_id = :groupId;';
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue('groupId', $variantGroupId);
     $stmt->execute();
     $nbAxes = $stmt->fetch()['nb'];
     $sql = 'SELECT v.entity_id ' . 'FROM pim_catalog_group_attribute as ga ' . "LEFT JOIN %product_value_table% as v ON v.attribute_id = ga.attribute_id " . 'WHERE ga.group_id = :groupId ' . 'GROUP BY v.entity_id ' . 'having count(v.option_id) = :nbAxes ;';
     $sql = QueryBuilderUtility::prepareDBALQuery($this->_em, $this->_entityName, $sql);
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue('groupId', $variantGroupId);
     $stmt->bindValue('nbAxes', $nbAxes);
     $stmt->execute();
     $results = $stmt->fetchAll();
     $productIds = array_map(function ($row) {
         return $row['entity_id'];
     }, $results);
     return $productIds;
 }
 /**
  * {@inheritdoc}
  */
 public function getEligibleProductIdsForVariantGroup($variantGroupId)
 {
     $sql = 'SELECT count(ga.attribute_id) as nb ' . 'FROM pim_catalog_group_attribute as ga ' . 'WHERE ga.group_id = :groupId;';
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue('groupId', $variantGroupId);
     $stmt->execute();
     $nbAxes = $stmt->fetch()['nb'];
     $elligibleProductsSQL = 'SELECT v.entity_id as product_id ' . 'FROM pim_catalog_group_attribute as ga ' . 'LEFT JOIN %product_value_table% as v ON v.attribute_id = ga.attribute_id ' . 'WHERE ga.group_id = :groupId ' . 'GROUP BY v.entity_id ' . 'having count(v.option_id) = :nbAxes';
     $alreadyInGroupSQL = 'SELECT p.id as product_id ' . 'FROM pim_catalog_product as p ' . 'JOIN pim_catalog_group_product as gp on p.id = gp.product_id ' . 'JOIN pim_catalog_group as g on g.id = gp.group_id ' . 'JOIN pim_catalog_group_type as gt on gt.id = g.type_id ' . 'WHERE gt.code = "VARIANT" ' . 'AND g.id != :groupId';
     $sql = sprintf('SELECT * FROM (%s) as p WHERE p.product_id NOT IN (%s);', $elligibleProductsSQL, $alreadyInGroupSQL);
     $sql = QueryBuilderUtility::prepareDBALQuery($this->_em, $this->_entityName, $sql);
     $stmt = $this->getEntityManager()->getConnection()->prepare($sql);
     $stmt->bindValue('groupId', $variantGroupId);
     $stmt->bindValue('nbAxes', $nbAxes);
     $stmt->execute();
     $results = $stmt->fetchAll();
     $productIds = array_map(function ($row) {
         return $row['product_id'];
     }, $results);
     return $productIds;
 }