/** * Move tree node * * @param Node $node * @param Node $newParent * @param Node $prevNode * @return void * @throws \Exception * @todo Use adapter for generate conditions */ public function move($node, $newParent, $prevNode = null) { $position = 1; $oldPath = $node->getData($this->_pathField); $newPath = $newParent->getData($this->_pathField); $newPath = $newPath . '/' . $node->getId(); $oldPathLength = strlen($oldPath); $newLevel = $newParent->getLevel() + 1; $levelDisposition = $newLevel - $node->getLevel(); $data = [$this->_levelField => new \Zend_Db_Expr("{$this->_levelField} + '{$levelDisposition}'"), $this->_pathField => new \Zend_Db_Expr("CONCAT('{$newPath}', RIGHT({$this->_pathField}, LENGTH({$this->_pathField}) - {$oldPathLength}))")]; $condition = $this->_conn->quoteInto("{$this->_pathField} REGEXP ?", "^{$oldPath}(/|\$)"); $this->_conn->beginTransaction(); $reorderData = [$this->_orderField => new \Zend_Db_Expr("{$this->_orderField} + 1")]; try { if ($prevNode && $prevNode->getId()) { $reorderCondition = "{$this->_orderField} > {$prevNode->getData($this->_orderField)}"; $position = $prevNode->getData($this->_orderField) + 1; } else { $reorderCondition = $this->_conn->quoteInto("{$this->_pathField} REGEXP ?", "^{$newParent->getData($this->_pathField)}/[0-9]+\$"); $select = $this->_conn->select()->from($this->_table, new \Zend_Db_Expr("MIN({$this->_orderField})"))->where($reorderCondition); $position = (int) $this->_conn->fetchOne($select); } $this->_conn->update($this->_table, $reorderData, $reorderCondition); $this->_conn->update($this->_table, $data, $condition); $this->_conn->update($this->_table, [$this->_orderField => $position, $this->_levelField => $newLevel], $this->_conn->quoteInto("{$this->_idField} = ?", $node->getId())); $this->_conn->commit(); } catch (\Exception $e) { $this->_conn->rollBack(); throw new \Exception("Can't move tree node due to error: " . $e->getMessage()); } }
/** * Updates data when subscriber received * * @param \Magento\Newsletter\Model\Subscriber $subscriber * @param \Magento\Newsletter\Model\Queue $queue * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ public function received(\Magento\Newsletter\Model\Subscriber $subscriber, \Magento\Newsletter\Model\Queue $queue) { $this->connection->beginTransaction(); try { $data['letter_sent_at'] = $this->_date->gmtDate(); $this->connection->update($this->_subscriberLinkTable, $data, ['subscriber_id = ?' => $subscriber->getId(), 'queue_id = ?' => $queue->getId()]); $this->connection->commit(); } catch (\Exception $e) { $this->connection->rollBack(); throw new \Magento\Framework\Exception\LocalizedException(__('We cannot mark as received subscriber.')); } return $this; }
/** * Update session * * @param string $sessionId * @param string $sessionData * @return bool */ public function write($sessionId, $sessionData) { // need to use write connection to get the most fresh DB sessions $bindValues = ['session_id' => $sessionId]; $select = $this->_write->select()->from($this->_sessionTable)->where('session_id = :session_id'); $exists = $this->_write->fetchOne($select, $bindValues); // encode session serialized data to prevent insertion of incorrect symbols $sessionData = base64_encode($sessionData); $bind = ['session_expires' => time(), 'session_data' => $sessionData]; if ($exists) { $this->_write->update($this->_sessionTable, $bind, ['session_id=?' => $sessionId]); } else { $bind['session_id'] = $sessionId; $this->_write->insert($this->_sessionTable, $bind); } return true; }
/** * @param Node $node * @return $this * @throws \Exception */ public function removeNode($node) { // For reorder old node branch $dataReorderOld = [$this->_orderField => new \Zend_Db_Expr($this->_conn->quoteIdentifier($this->_orderField) . '-1')]; $conditionReorderOld = $this->_conn->quoteIdentifier($this->_parentField) . '=' . $node->getData($this->_parentField) . ' AND ' . $this->_conn->quoteIdentifier($this->_orderField) . '>' . $node->getData($this->_orderField); $this->_conn->beginTransaction(); try { $condition = $this->_conn->quoteInto("{$this->_idField}=?", $node->getId()); $this->_conn->delete($this->_table, $condition); // Update old node branch $this->_conn->update($this->_table, $dataReorderOld, $conditionReorderOld); $this->_conn->commit(); } catch (\Exception $e) { $this->_conn->rollBack(); throw new \Exception('Can\'t remove tree node'); } parent::removeNode($node); return $this; }
/** * Update suffix for url rewrites * * @return $this */ protected function updateSuffixForUrlRewrites() { $map = [ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX => ProductUrlRewriteGenerator::ENTITY_TYPE, CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX => CategoryUrlRewriteGenerator::ENTITY_TYPE]; if (!isset($map[$this->getPath()])) { return $this; } $dataFilter = [UrlRewrite::ENTITY_TYPE => $map[$this->getPath()]]; $storesIds = $this->getStoreIds(); if ($storesIds) { $dataFilter[UrlRewrite::STORE_ID] = $storesIds; } $entities = $this->urlFinder->findAllByData($dataFilter); $oldSuffixPattern = '~' . preg_quote($this->getOldValue()) . '$~'; $suffix = $this->getValue(); foreach ($entities as $urlRewrite) { $bind = $urlRewrite->getIsAutogenerated() ? [UrlRewrite::REQUEST_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getRequestPath())] : [UrlRewrite::TARGET_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getTargetPath())]; $this->connection->update(DbStorage::TABLE_NAME, $bind, $this->connection->quoteIdentifier(UrlRewrite::URL_REWRITE_ID) . ' = ' . $urlRewrite->getUrlRewriteId()); } return $this; }
/** * Updates document rows with specified data based on a WHERE clause * * @param mixed $document * @param array $bind * @param mixed $where * @return int */ public function updateDocument($document, array $bind, $where = '') { return $this->resourceAdapter->update($document, $bind, $where); }