/** * @param array $linksData * @return void */ public function saveLinksData($linksData) { $mainTable = $this->productLink->getMainTable(); $relationTable = $this->productLink->getTable('catalog_product_relation'); // save links and relations if ($linksData['product_ids']) { $this->deleteOldLinks(array_keys($linksData['product_ids'])); $mainData = []; foreach ($linksData['relation'] as $productData) { $mainData[] = ['product_id' => $productData['parent_id'], 'linked_product_id' => $productData['child_id'], 'link_type_id' => $this->getLinkTypeId()]; } $this->connection->insertOnDuplicate($mainTable, $mainData); $this->connection->insertOnDuplicate($relationTable, $linksData['relation']); } $attributes = $this->getAttributes(); // save positions and default quantity if ($linksData['attr_product_ids']) { $savedData = $this->connection->fetchPairs($this->connection->select()->from($mainTable, [new \Zend_Db_Expr('CONCAT_WS(" ", product_id, linked_product_id)'), 'link_id'])->where('product_id IN (?) AND link_type_id = ' . $this->connection->quote($this->getLinkTypeId()), array_keys($linksData['attr_product_ids']))); foreach ($savedData as $pseudoKey => $linkId) { if (isset($linksData['position'][$pseudoKey])) { $linksData['position'][$pseudoKey]['link_id'] = $linkId; } if (isset($linksData['qty'][$pseudoKey])) { $linksData['qty'][$pseudoKey]['link_id'] = $linkId; } } if (!empty($linksData['position'])) { $this->connection->insertOnDuplicate($attributes['position']['table'], $linksData['position']); } if (!empty($linksData['qty'])) { $this->connection->insertOnDuplicate($attributes['qty']['table'], $linksData['qty']); } } }
/** * Prepare website current dates table * * @return \Magento\Catalog\Model\Indexer\Product\Price\AbstractAction */ protected function _prepareWebsiteDateTable() { $baseCurrency = $this->_config->getValue(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE); $select = $this->_connection->select()->from(['cw' => $this->_defaultIndexerResource->getTable('store_website')], ['website_id'])->join(['csg' => $this->_defaultIndexerResource->getTable('store_group')], 'cw.default_group_id = csg.group_id', ['store_id' => 'default_store_id'])->where('cw.website_id != 0'); $data = []; foreach ($this->_connection->fetchAll($select) as $item) { /** @var $website \Magento\Store\Model\Website */ $website = $this->_storeManager->getWebsite($item['website_id']); if ($website->getBaseCurrencyCode() != $baseCurrency) { $rate = $this->_currencyFactory->create()->load($baseCurrency)->getRate($website->getBaseCurrencyCode()); if (!$rate) { $rate = 1; } } else { $rate = 1; } /** @var $store \Magento\Store\Model\Store */ $store = $this->_storeManager->getStore($item['store_id']); if ($store) { $timestamp = $this->_localeDate->scopeTimeStamp($store); $data[] = ['website_id' => $website->getId(), 'website_date' => $this->_dateTime->formatDate($timestamp, false), 'rate' => $rate]; } } $table = $this->_defaultIndexerResource->getTable('catalog_product_index_website'); $this->_emptyTable($table); if ($data) { foreach ($data as $row) { $this->_connection->insertOnDuplicate($table, $row, array_keys($row)); } } return $this; }
/** * Assert that session data reads from DB correctly regardless of encoding * * @param string $sessionData * * @dataProvider readEncodedDataProvider */ public function testReadEncoded($sessionData) { $sessionRecord = [self::COLUMN_SESSION_ID => self::SESSION_ID, self::COLUMN_SESSION_DATA => $sessionData]; $this->_connection->insertOnDuplicate($this->_sessionTable, $sessionRecord, [self::COLUMN_SESSION_DATA]); $sessionData = $this->_model->read(self::SESSION_ID); $this->assertEquals($this->_sourceData[self::SESSION_NEW], unserialize($sessionData)); }
/** * @dataProvider insertDataProvider */ public function testInsertOnDuplicate($data) { $this->_connection->insertOnDuplicate($this->_tableName, $data); $select = $this->_connection->select()->from($this->_tableName); $result = $this->_connection->fetchRow($select); $this->assertEquals($data, $result); }
/** * @param array $documents * @param Dimension[] $dimensions * @return void */ protected function insertDocumentsForFilterable(array $documents, array $dimensions) { $onDuplicate = []; foreach ($this->fields as $field) { if ($field['type'] === $this->dataTypes[1]) { $onDuplicate[] = $field['name']; } } $this->connection->insertOnDuplicate($this->getTableName($this->dataTypes[1], $dimensions), $this->prepareFilterableFields($documents), $onDuplicate); }
/** * @param array $arrData * @return $this * @throws \Exception */ protected function saveRuleProductPrices($arrData) { if (empty($arrData)) { return $this; } $productIds = []; try { foreach ($arrData as $key => $data) { $productIds['product_id'] = $data['product_id']; $arrData[$key]['rule_date'] = $this->dateFormat->formatDate($data['rule_date'], false); $arrData[$key]['latest_start_date'] = $this->dateFormat->formatDate($data['latest_start_date'], false); $arrData[$key]['earliest_end_date'] = $this->dateFormat->formatDate($data['earliest_end_date'], false); } $this->connection->insertOnDuplicate($this->getTable('catalogrule_product_price'), $arrData); } catch (\Exception $e) { throw $e; } return $this; }
/** * Insert selections. * * @return \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType */ protected function insertSelections() { $selectionTable = $this->_resource->getTableName('catalog_product_bundle_selection'); $selections = []; foreach ($this->_cachedOptions as $productId => $options) { foreach ($options as $option) { $index = 0; foreach ($option['selections'] as $selection) { if (isset($selection['position'])) { $index = $selection['position']; } if ($tmpArray = $this->populateSelectionTemplate($selection, $option['option_id'], $productId, $index)) { $selections[] = $tmpArray; $index++; } } } } if (!empty($selections)) { $this->connection->insertOnDuplicate($selectionTable, $selections, ['selection_id', 'product_id', 'position', 'is_default', 'selection_price_type', 'selection_price_value', 'selection_qty', 'selection_can_change_qty']); } return $this; }
/** * Collected link data insertion. * * @return $this * @throws \Zend_Db_Exception */ protected function _insertData() { $mainTable = $this->_resource->getTableName('catalog_product_super_attribute'); $labelTable = $this->_resource->getTableName('catalog_product_super_attribute_label'); $linkTable = $this->_resource->getTableName('catalog_product_super_link'); $relationTable = $this->_resource->getTableName('catalog_product_relation'); $mainData = []; foreach ($this->_superAttributesData['attributes'] as $productId => $attributesData) { foreach ($attributesData as $attrId => $row) { $row['product_id'] = $productId; $row['attribute_id'] = $attrId; $mainData[] = $row; } } if ($mainData) { $this->_connection->insertOnDuplicate($mainTable, $mainData); } if ($this->_superAttributesData['labels']) { $this->_connection->insertOnDuplicate($labelTable, $this->_superAttributesData['labels']); } if ($this->_superAttributesData['super_link']) { $this->_connection->insertOnDuplicate($linkTable, $this->_superAttributesData['super_link']); } if ($this->_superAttributesData['relation']) { $this->_connection->insertOnDuplicate($relationTable, $this->_superAttributesData['relation']); } return $this; }
/** * @inheritdoc */ public function updateChangedRecords($document, $data) { return $this->resourceAdapter->insertOnDuplicate($document, $data); }
/** * Update product data which related to custom options information * * @param array $data Product data which will be updated * @return $this */ protected function _updateProducts(array $data) { if ($data) { $this->_connection->insertOnDuplicate($this->_tables['catalog_product_entity'], $data, array('has_options', 'required_options', 'updated_at')); } return $this; }
/** * Write single product into flat product table * * @param int $storeId * @param int $productId * @param string $valueFieldSuffix * @return \Magento\Catalog\Model\Indexer\Product\Flat */ public function write($storeId, $productId, $valueFieldSuffix = '') { $flatTable = $this->_productIndexerHelper->getFlatTableName($storeId); $attributes = $this->_productIndexerHelper->getAttributes(); $eavAttributes = $this->_productIndexerHelper->getTablesStructure($attributes); $updateData = array(); $describe = $this->_connection->describeTable($flatTable); foreach ($eavAttributes as $tableName => $tableColumns) { $columnsChunks = array_chunk($tableColumns, self::ATTRIBUTES_CHUNK_SIZE, true); foreach ($columnsChunks as $columns) { $select = $this->_connection->select(); $selectValue = $this->_connection->select(); $keyColumns = array('entity_id' => 'e.entity_id', 'attribute_id' => 't.attribute_id', 'value' => $this->_connection->getIfNullSql('`t2`.`value`', '`t`.`value`')); if ($tableName != $this->_productIndexerHelper->getTable('catalog_product_entity')) { $valueColumns = array(); $ids = array(); $select->from(array('e' => $this->_productIndexerHelper->getTable('catalog_product_entity')), $keyColumns); $selectValue->from(array('e' => $this->_productIndexerHelper->getTable('catalog_product_entity')), $keyColumns); /** @var $attribute \Magento\Catalog\Model\Resource\Eav\Attribute */ foreach ($columns as $columnName => $attribute) { if (isset($describe[$columnName])) { $ids[$attribute->getId()] = $columnName; } } $select->joinLeft(array('t' => $tableName), 'e.entity_id = t.entity_id ' . $this->_connection->quoteInto(' AND t.attribute_id IN (?)', array_keys($ids)) . ' AND t.store_id = 0', array())->joinLeft(array('t2' => $tableName), 't.entity_id = t2.entity_id ' . ' AND t.attribute_id = t2.attribute_id ' . $this->_connection->quoteInto(' AND t2.store_id = ?', $storeId), array())->where('e.entity_id = ' . $productId)->where('t.attribute_id IS NOT NULL'); $cursor = $this->_connection->query($select); while ($row = $cursor->fetch(\Zend_Db::FETCH_ASSOC)) { $updateData[$ids[$row['attribute_id']]] = $row['value']; $valueColumnName = $ids[$row['attribute_id']] . $valueFieldSuffix; if (isset($describe[$valueColumnName])) { $valueColumns[$row['value']] = $valueColumnName; } } //Update not simple attributes (eg. dropdown) if (!empty($valueColumns)) { $valueIds = array_keys($valueColumns); $select = $this->_connection->select()->from(array('t' => $this->_productIndexerHelper->getTable('eav_attribute_option_value')), array('t.option_id', 't.value'))->where($this->_connection->quoteInto('t.option_id IN (?)', $valueIds)); $cursor = $this->_connection->query($select); while ($row = $cursor->fetch(\Zend_Db::FETCH_ASSOC)) { $valueColumnName = $valueColumns[$row['option_id']]; if (isset($describe[$valueColumnName])) { $updateData[$valueColumnName] = $row['value']; } } } } else { $columnNames = array_keys($columns); $columnNames[] = 'attribute_set_id'; $columnNames[] = 'type_id'; $select->from(array('e' => $this->_productIndexerHelper->getTable('catalog_product_entity')), $columnNames)->where('e.entity_id = ' . $productId); $cursor = $this->_connection->query($select); $row = $cursor->fetch(\Zend_Db::FETCH_ASSOC); if (!empty($row)) { foreach ($row as $columnName => $value) { $updateData[$columnName] = $value; } } } } } if (!empty($updateData)) { $updateData += array('entity_id' => $productId); $updateFields = array(); foreach ($updateData as $key => $value) { $updateFields[$key] = $key; } $this->_connection->insertOnDuplicate($flatTable, $updateData, $updateFields); } return $this; }