/** * Load customer log data by customer id * * @param int $customerId * @return array */ protected function loadLogData($customerId) { /** @var \Magento\Framework\DB\Adapter\AdapterInterface $connection */ $connection = $this->resource->getConnection(); $select = $connection->select()->from(['cl' => $this->resource->getTableName('customer_log')])->joinLeft(['cv' => $this->resource->getTableName('customer_visitor')], 'cv.customer_id = cl.customer_id', ['last_visit_at'])->where('cl.customer_id = ?', $customerId)->order('cv.visitor_id DESC')->limit(1); return $connection->fetchRow($select); }
/** * Retrieve write connection instance * * @return bool|\Magento\Framework\DB\Adapter\AdapterInterface */ protected function _getConnection() { if (null === $this->_connection) { $this->_connection = $this->_resource->getConnection(); } return $this->_connection; }
/** * @param ResourceConnection $resource * @param string $targetColumn * @param string $targetTable * @param array $referenceColumns * @param array $sortOrder */ public function __construct(ResourceConnection $resource, $targetColumn, $targetTable, array $referenceColumns = [], array $sortOrder = []) { $this->targetTable = $targetTable; $this->targetColumn = $targetColumn; $this->referenceColumns = $referenceColumns; $this->sortOrder = $sortOrder; $this->resource = $resource; $this->adapter = $this->resource->getConnection(); }
/** * Retrieve customer IDs for reindex * * @return array */ protected function getCustomerIdsForReindex() { $connection = $this->resource->getConnection(); $gridTableName = $this->flatScopeResolver->resolve(Customer::CUSTOMER_GRID_INDEXER_ID, []); $select = $connection->select()->from($this->resource->getTableName($gridTableName), 'last_visit_at')->order('last_visit_at DESC')->limit(1); $lastVisitAt = $connection->query($select)->fetchColumn(); $select = $connection->select()->from($this->resource->getTableName('customer_log'), 'customer_id')->where('last_login_at > ?', $lastVisitAt); $customerIds = []; foreach ($connection->query($select)->fetchAll() as $row) { $customerIds[] = $row['customer_id']; } return $customerIds; }
/** * Build index query * * @param RequestInterface $request * @return Select */ public function build(RequestInterface $request) { $searchIndexTable = $this->scopeResolver->resolve($request->getIndex(), $request->getDimensions()); $select = $this->resource->getConnection()->select()->from(['search_index' => $searchIndexTable], ['entity_id' => 'entity_id'])->joinLeft(['cea' => $this->resource->getTableName('catalog_eav_attribute')], 'search_index.attribute_id = cea.attribute_id', []); $select = $this->tableMapper->addTables($select, $request); $select = $this->processDimensions($request, $select); $isShowOutOfStock = $this->config->isSetFlag('cataloginventory/options/show_out_of_stock', ScopeInterface::SCOPE_STORE); if ($isShowOutOfStock === false) { $select->joinLeft(['stock_index' => $this->resource->getTableName('cataloginventory_stock_status')], 'search_index.entity_id = stock_index.product_id' . $this->resource->getConnection()->quoteInto(' AND stock_index.website_id = ?', $this->storeManager->getWebsite()->getId()), []); $select->where('stock_index.stock_status = ?', 1); } return $select; }
/** * @param array $attributes * @return array */ public function filter(array $attributes) { $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID); if ($indexer->getState()->getStatus() != StateInterface::STATUS_VALID) { $tableName = $this->flatScopeResolver->resolve(Customer::CUSTOMER_GRID_INDEXER_ID, []); $columns = $this->resource->getConnection()->describeTable($tableName); foreach (array_keys($attributes) as $attributeCode) { if (!isset($columns[$attributeCode])) { unset($attributes[$attributeCode]); } } } return $attributes; }
/** * Check attribute lock state * * @param \Magento\Framework\Model\AbstractModel $object * @param null $attributeSet * @throws \Magento\Framework\Exception\LocalizedException * @return void */ public function validate(\Magento\Framework\Model\AbstractModel $object, $attributeSet = null) { $metadata = $this->metadataPool->getMetadata(ProductInterface::class); $connection = $this->resource->getConnection(); $bind = ['attribute_id' => $object->getAttributeId()]; $select = clone $connection->select(); $select->reset()->from(['main_table' => $this->resource->getTableName('catalog_product_super_attribute')], ['psa_count' => 'COUNT(product_super_attribute_id)'])->join(['entity' => $this->resource->getTableName('catalog_product_entity')], 'main_table.product_id = entity.' . $metadata->getLinkField())->where('main_table.attribute_id = :attribute_id')->group('main_table.attribute_id')->limit(1); if ($attributeSet !== null) { $bind['attribute_set_id'] = $attributeSet; $select->where('entity.attribute_set_id = :attribute_set_id'); } if ($connection->fetchOne($select, $bind)) { throw new \Magento\Framework\Exception\LocalizedException(__('This attribute is used in configurable products.')); } }
/** * @param array $documents * @param Dimension[] $dimensions * @return void */ private function insertDocuments(array $documents, array $dimensions) { $documents = $this->prepareSearchableFields($documents); if (empty($documents)) { return; } $this->resource->getConnection()->insertOnDuplicate($this->getTableName($dimensions), $documents, ['data_index']); }
/** * @param \Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider $subject * @param callable|\Closure $proceed * @param BucketInterface $bucket * @param Dimension[] $dimensions * * @param Table $entityIdsTable * @return Select * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundGetDataSet(\Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider $subject, \Closure $proceed, BucketInterface $bucket, array $dimensions, Table $entityIdsTable) { if ($bucket->getField() == 'category_ids') { $currentScopeId = $this->scopeResolver->getScope($dimensions['scope']->getValue())->getId(); $currentCategory = $this->layer->getCurrentCategory(); $derivedTable = $this->resource->getConnection()->select(); $derivedTable->from(['main_table' => $this->resource->getTableName('catalog_category_product_index')], ['value' => 'category_id'])->where('main_table.store_id = ?', $currentScopeId); $derivedTable->joinInner(['entities' => $entityIdsTable->getName()], 'main_table.product_id = entities.entity_id', []); if (!empty($currentCategory)) { $derivedTable->join(['category' => $this->resource->getTableName('catalog_category_entity')], 'main_table.category_id = category.entity_id', [])->where('`category`.`path` LIKE ?', $currentCategory->getPath() . '%')->where('`category`.`level` > ?', $currentCategory->getLevel()); } $select = $this->resource->getConnection()->select(); $select->from(['main_table' => $derivedTable]); return $select; } return $proceed($bucket, $dimensions, $entityIdsTable); }
/** * Retrieve connection to resource specified by $resourceName. * * @param string $resourceName * @return \Exception|false|\Magento\Framework\DB\Adapter\AdapterInterface|\Zend_Exception */ protected function getConnection($resourceName) { try { $connection = $this->resource->getConnection($resourceName); return $connection; } catch (\Zend_Exception $e) { echo $e->getMessage() . PHP_EOL; return $e; } }
/** * @param string $tableName * @param array $fields * @throws \Zend_Db_Exception * @return void */ protected function createFlatIndex($tableName, array $fields) { $table = $this->resource->getConnection()->newTable($tableName); $table->addColumn('entity_id', Table::TYPE_INTEGER, 10, ['unsigned' => true, 'nullable' => false], 'Entity ID'); foreach ($fields as $field) { if ($field['type'] !== 'filterable') { continue; } $columnMap = isset($field['dataType']) && isset($this->columnTypesMap[$field['dataType']]) ? $this->columnTypesMap[$field['dataType']] : ['type' => $field['type'], 'size' => isset($field['size']) ? $field['size'] : null]; $name = $field['name']; $type = $columnMap['type']; $size = $columnMap['size']; $table->addColumn($name, $type, $size); } $this->resource->getConnection()->createTable($table); }
/** * @param string $field * @return array|null */ private function getFieldToTableMap($field) { $fieldToTableMap = ['price' => ['price_index', 'catalog_product_index_price', $this->resource->getConnection()->quoteInto('search_index.entity_id = price_index.entity_id AND price_index.website_id = ?', $this->getWebsiteId()), []], 'category_ids' => ['category_ids_index', 'catalog_category_product_index', 'search_index.entity_id = category_ids_index.product_id', []]]; return array_key_exists($field, $fieldToTableMap) ? $fieldToTableMap[$field] : null; }
/** * Get Connection * * @return AdapterInterface */ private function getConnection() { return $this->resource->getConnection(); }
/** * @return false|AdapterInterface */ private function getAdapter() { $adapter = $this->resource->getConnection('write'); return $adapter; }
/** * @param string $tableName * @throws \Zend_Db_Exception * @return void */ protected function createFulltextIndex($tableName) { $table = $this->resource->getConnection()->newTable($tableName)->addColumn('entity_id', Table::TYPE_INTEGER, 10, ['unsigned' => true, 'nullable' => false], 'Entity ID')->addColumn('attribute_id', Table::TYPE_INTEGER, 10, ['unsigned' => true, 'nullable' => false])->addColumn('data_index', Table::TYPE_TEXT, '4g', ['nullable' => true], 'Data index')->addIndex('idx_primary', ['entity_id', 'attribute_id'], ['type' => AdapterInterface::INDEX_TYPE_PRIMARY])->addIndex('FTI_FULLTEXT_DATA_INDEX', ['data_index'], ['type' => AdapterInterface::INDEX_TYPE_FULLTEXT]); $this->resource->getConnection()->createTable($table); }