Пример #1
0
 /**
  * Delete products from flat table(s)
  *
  * @param int|array $productId
  * @param null|int $storeId
  * @return void
  */
 public function deleteProductsFromStore($productId, $storeId = null)
 {
     if (!is_array($productId)) {
         $productId = [$productId];
     }
     if (null === $storeId) {
         foreach ($this->storeManager->getStores() as $store) {
             $this->connection->delete($this->productIndexerHelper->getFlatTableName($store->getId()), ['entity_id IN(?)' => $productId]);
         }
     } else {
         $this->connection->delete($this->productIndexerHelper->getFlatTableName((int) $storeId), ['entity_id IN(?)' => $productId]);
     }
 }
Пример #2
0
 /**
  * Apply diff. between 0 store and current store to temporary flat table
  *
  * @param array $tables
  * @param array $changedIds
  * @param int|string $storeId
  * @param string $valueFieldSuffix
  * @return void
  */
 protected function _updateTemporaryTableByStoreValues(array $tables, array $changedIds, $storeId, $valueFieldSuffix)
 {
     $flatColumns = $this->_productIndexerHelper->getFlatColumns();
     $temporaryFlatTableName = $this->_getTemporaryTableName($this->_productIndexerHelper->getFlatTableName($storeId));
     $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
     foreach ($tables as $tableName => $columns) {
         foreach ($columns as $attribute) {
             /* @var $attribute \Magento\Eav\Model\Entity\Attribute */
             $attributeCode = $attribute->getAttributeCode();
             if ($attribute->getBackend()->getType() != 'static') {
                 $joinCondition = sprintf('t.%s = e.%s', $linkField, $linkField) . ' AND t.attribute_id=' . $attribute->getId() . ' AND t.store_id = ' . $storeId . ' AND t.value IS NOT NULL';
                 /** @var $select \Magento\Framework\DB\Select */
                 $select = $this->_connection->select()->joinInner(['t' => $tableName], $joinCondition, [$attributeCode => 't.value']);
                 if (!empty($changedIds)) {
                     $select->where($this->_connection->quoteInto('e.entity_id IN (?)', $changedIds));
                 }
                 $sql = $select->crossUpdateFromSelect(['e' => $temporaryFlatTableName]);
                 $this->_connection->query($sql);
             }
             //Update not simple attributes (eg. dropdown)
             if (isset($flatColumns[$attributeCode . $valueFieldSuffix])) {
                 $select = $this->_connection->select()->joinInner(['t' => $this->_productIndexerHelper->getTable('eav_attribute_option_value')], 't.option_id = e.' . $attributeCode . ' AND t.store_id=' . $storeId, [$attributeCode . $valueFieldSuffix => 't.value']);
                 if (!empty($changedIds)) {
                     $select->where($this->_connection->quoteInto('e.entity_id IN (?)', $changedIds));
                 }
                 $sql = $select->crossUpdateFromSelect(['e' => $temporaryFlatTableName]);
                 $this->_connection->query($sql);
             }
         }
     }
 }
Пример #3
0
 /**
  * Check is flat table for store exists
  *
  * @param int $storeId
  * @return bool
  */
 protected function _isFlatTableExists($storeId)
 {
     if (!isset($this->_flatTablesExist[$storeId])) {
         $tableName = $this->_productIndexerHelper->getFlatTableName($storeId);
         $isTableExists = $this->_connection->isTableExists($tableName);
         $this->_flatTablesExist[$storeId] = $isTableExists ? true : false;
     }
     return $this->_flatTablesExist[$storeId];
 }
Пример #4
0
 /**
  * Test deleting changelog table
  */
 public function testDeleteCl()
 {
     $this->_changelogMock->expects($this->any())->method('getName')->will($this->returnValue('catalog_product_flat_cl'));
     $this->_connectionMock->expects($this->once())->method('getTables')->with('catalog_product_flat_%')->will($this->returnValue(['catalog_product_flat_cl']));
     $this->_connectionMock->expects($this->never())->method('dropTable');
     $this->_resourceMock->expects($this->once())->method('getConnection')->with('write')->will($this->returnValue($this->_connectionMock));
     $this->_setStoreManagerExpectedStores([1]);
     $this->_model->deleteAbandonedStoreFlatTables();
 }
Пример #5
0
 /**
  * Move data from temporary tables to flat
  *
  * @param string $flatTable
  * @param string $flatDropName
  * @param string $temporaryFlatTableName
  * @return void
  */
 public function move($flatTable, $flatDropName, $temporaryFlatTableName)
 {
     $connection = $this->_resource->getConnection();
     if (!$connection->isTableExists($flatTable)) {
         $connection->dropTable($flatDropName);
         $connection->renameTablesBatch([['oldName' => $temporaryFlatTableName, 'newName' => $flatTable]]);
         $connection->dropTable($flatDropName);
     } else {
         $describe = $connection->describeTable($flatTable);
         $columns = $this->_productIndexerHelper->getFlatColumns();
         $columns = array_keys(array_intersect_key($describe, $columns));
         $select = $connection->select();
         $select->from(['tf' => sprintf('%s_tmp_indexer', $flatTable)], $columns);
         $sql = $select->insertFromSelect($flatTable, $columns);
         $connection->query($sql);
         $connection->dropTable(sprintf('%s_tmp_indexer', $flatTable));
     }
 }
Пример #6
0
 public function testMoveWithExistentFlatTable()
 {
     $flatTable = 'flat_table';
     $flatDropName = 'flat_table_to_drop';
     $temporaryFlatTableName = 'flat_tmp';
     $describedColumns = array('column_11' => 'column_definition', 'column_2' => 'column_definition', 'column_3' => 'column_definition');
     $flatColumns = array('column_1' => 'column_definition', 'column_2' => 'column_definition', 'column_3' => 'column_definition');
     $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', array(), array(), '', false);
     $selectMock->expects($this->once())->method('from')->with(array('tf' => sprintf('%s_tmp_indexer', $flatTable)), array('column_2', 'column_3'));
     $sql = md5(time());
     $selectMock->expects($this->once())->method('insertFromSelect')->with($flatTable, array('column_2', 'column_3'))->will($this->returnValue($sql));
     $this->_connectionMock->expects($this->once())->method('query')->with($sql);
     $this->_connectionMock->expects($this->once())->method('select')->will($this->returnValue($selectMock));
     $this->_connectionMock->expects($this->once())->method('isTableExists')->with($flatTable)->will($this->returnValue(true));
     $this->_connectionMock->expects($this->once())->method('describeTable')->with($flatTable)->will($this->returnValue($describedColumns));
     $this->_productIndexerHelper->expects($this->once())->method('getFlatColumns')->will($this->returnValue($flatColumns));
     $this->_connectionMock->expects($this->once())->method('dropTable')->with(sprintf('%s_tmp_indexer', $flatTable));
     $this->_resourceMock->expects($this->any())->method('getConnection')->with('write')->will($this->returnValue($this->_connectionMock));
     $model = $this->_objectManager->getObject('Magento\\Catalog\\Model\\Indexer\\Product\\Flat\\Action\\Rows\\TableData', array('resource' => $this->_resourceMock, 'productIndexerHelper' => $this->_productIndexerHelper));
     $model->move($flatTable, $flatDropName, $temporaryFlatTableName);
 }
Пример #7
0
 /**
  * Fill temporary table by data from products EAV attributes by type
  *
  * @param string $tableName
  * @param array  $tableColumns
  * @param array  $changedIds
  * @param string $valueFieldSuffix
  * @param int $storeId
  * @return void
  */
 protected function _fillTemporaryTable($tableName, array $tableColumns, array $changedIds, $valueFieldSuffix, $storeId)
 {
     $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
     if (!empty($tableColumns)) {
         $columnsChunks = array_chunk($tableColumns, Action\Indexer::ATTRIBUTES_CHUNK_SIZE, true);
         foreach ($columnsChunks as $columnsList) {
             $select = $this->_connection->select();
             $selectValue = $this->_connection->select();
             $entityTableName = $this->_getTemporaryTableName($this->_productIndexerHelper->getTable('catalog_product_entity'));
             $temporaryTableName = $this->_getTemporaryTableName($tableName);
             $temporaryValueTableName = $temporaryTableName . $valueFieldSuffix;
             $keyColumn = ['entity_id'];
             $columns = array_merge($keyColumn, array_keys($columnsList));
             $valueColumns = $keyColumn;
             $flatColumns = $this->_productIndexerHelper->getFlatColumns();
             $iterationNum = 1;
             $select->from(['et' => $entityTableName], $keyColumn)->join(['e' => $this->resource->getTableName('catalog_product_entity')], 'e.entity_id = et.entity_id', []);
             $selectValue->from(['e' => $temporaryTableName], $keyColumn);
             /** @var $attribute \Magento\Catalog\Model\ResourceModel\Eav\Attribute */
             foreach ($columnsList as $columnName => $attribute) {
                 $countTableName = 't' . $iterationNum++;
                 $joinCondition = sprintf('e.%3$s = %1$s.%3$s AND %1$s.attribute_id = %2$d AND %1$s.store_id = 0', $countTableName, $attribute->getId(), $metadata->getLinkField());
                 $select->joinLeft([$countTableName => $tableName], $joinCondition, [$columnName => 'value']);
                 if ($attribute->getFlatUpdateSelect($storeId) instanceof \Magento\Framework\DB\Select) {
                     $attributeCode = $attribute->getAttributeCode();
                     $columnValueName = $attributeCode . $valueFieldSuffix;
                     if (isset($flatColumns[$columnValueName])) {
                         $valueJoinCondition = sprintf('e.%1$s = %2$s.option_id AND %2$s.store_id = 0', $attributeCode, $countTableName);
                         $selectValue->joinLeft([$countTableName => $this->_productIndexerHelper->getTable('eav_attribute_option_value')], $valueJoinCondition, [$columnValueName => $countTableName . '.value']);
                         $valueColumns[] = $columnValueName;
                     }
                 }
             }
             if (!empty($changedIds)) {
                 $select->where($this->_connection->quoteInto('e.entity_id IN (?)', $changedIds));
             }
             $sql = $select->insertFromSelect($temporaryTableName, $columns, true);
             $this->_connection->query($sql);
             if (count($valueColumns) > 1) {
                 if (!empty($changedIds)) {
                     $selectValue->where($this->_connection->quoteInto('e.entity_id IN (?)', $changedIds));
                 }
                 $sql = $selectValue->insertFromSelect($temporaryValueTableName, $valueColumns, true);
                 $this->_connection->query($sql);
             }
         }
     }
 }
 /**
  * Delete all product flat tables for not existing stores
  *
  * @return void
  */
 public function execute()
 {
     $this->indexer->deleteAbandonedStoreFlatTables();
 }
Пример #9
0
 public function testIsAddChildDataDefault()
 {
     $this->assertEquals(0, $this->_helper->isAddChildData());
 }
Пример #10
0
 /**
  * Is original attribute enabled for flat indexing
  *
  * @return bool
  */
 protected function _isOriginalEnabledInFlat()
 {
     return $this->getOrigData('backend_type') == 'static' || $this->_productFlatIndexerHelper->isAddFilterableAttributes() && $this->getOrigData('is_filterable') > 0 || $this->getOrigData('used_in_product_listing') == 1 || $this->getOrigData('used_for_sort_by') == 1;
 }
Пример #11
0
 /**
  * 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;
 }