/** * Retrieves Metadata for entity by entity type and store id * * @param string $entityType * @param int $storeId * @return \Magento\SalesSequence\Model\Meta * @throws \Magento\Framework\Exception\LocalizedException */ public function loadByEntityTypeAndStore($entityType, $storeId) { $meta = $this->metaFactory->create(); $connection = $this->getConnection(); $bind = ['entity_type' => $entityType, 'store_id' => $storeId]; $select = $connection->select()->from($this->getMainTable(), [$this->getIdFieldName()])->where('entity_type = :entity_type AND store_id = :store_id'); $metaId = $connection->fetchOne($select, $bind); if ($metaId) { $this->load($meta, $metaId); } return $meta; }
/** * Create sequence with metadata and profile * * @throws \Exception * @throws \Magento\Framework\Exception\AlreadyExistsException * @return void */ public function create() { $metadata = $this->resourceMetadata->loadByEntityTypeAndStore($this->data['entity_type'], $this->data['store_id']); if ($metadata->getSequenceTable() == $this->getSequenceName()) { return; } $this->data['sequence_table'] = $this->getSequenceName(); $this->data['is_active'] = 1; $profile = $this->profileFactory->create(['data' => array_intersect_key($this->data, array_flip(['prefix', 'suffix', 'start_value', 'step', 'max_value', 'warning_value', 'is_active', 'active_profile']))]); $profile->setHasDataChanges(true); $this->data['active_profile'] = $profile; $metadata = $this->metaFactory->create(['data' => array_intersect_key($this->data, array_flip(['entity_type', 'store_id', 'sequence_table', 'active_profile']))]); $metadata->setHasDataChanges(true); try { $this->resourceMetadata->save($metadata); $adapter = $this->appResource->getConnection('write'); if (!$adapter->isTableExists($this->data['sequence_table'])) { $adapter->query($this->ddlSequence->getCreateSequenceDdl($this->data['sequence_table'], $this->data['start_value'])); } } catch (Exception $e) { $this->resourceMetadata->delete($metadata); $this->logger->critical($e); throw $e; } $this->data = array_flip($this->pattern); }