/** * Get Target Path * * @param \Magento\UrlRewrite\Model\UrlRewrite $model * @return string * @throws \Magento\Framework\Exception\LocalizedException */ protected function getTargetPath($model) { $targetPath = $this->getCanonicalTargetPath(); if ($model->getRedirectType() && !$model->getIsAutogenerated()) { $data = [UrlRewrite::ENTITY_ID => $model->getEntityId(), UrlRewrite::TARGET_PATH => $targetPath, UrlRewrite::ENTITY_TYPE => $model->getEntityType(), UrlRewrite::STORE_ID => $model->getStoreId()]; $rewrite = $this->urlFinder->findOneByData($data); if (!$rewrite) { $message = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT ? __('The product you chose is not associated with the selected store or category.') : __('The category you chose is not associated with the selected store.'); throw new LocalizedException($message); } $targetPath = $rewrite->getRequestPath(); } return $targetPath; }
/** * Load rewrite information for request * If $path is array - we must load all possible records and choose one matching earlier record in array * * @param \Magento\UrlRewrite\Model\UrlRewrite $object * @param array|string $path * @return $this */ public function loadByRequestPath(\Magento\UrlRewrite\Model\UrlRewrite $object, $path) { if (!is_array($path)) { $path = array($path); } $pathBind = array(); foreach ($path as $key => $url) { $pathBind['path' . $key] = $url; } // Form select $adapter = $this->_getReadAdapter(); $select = $adapter->select()->from($this->getMainTable())->where('request_path IN (:' . implode(', :', array_flip($pathBind)) . ')')->where('store_id IN(?)', array(\Magento\Store\Model\Store::DEFAULT_STORE_ID, (int) $object->getStoreId())); $items = $adapter->fetchAll($select, $pathBind); // Go through all found records and choose one with lowest penalty - earlier path in array, concrete store $mapPenalty = array_flip(array_values($path)); // we got mapping array(path => index), lower index - better $currentPenalty = null; $foundItem = null; foreach ($items as $item) { if (!array_key_exists($item['request_path'], $mapPenalty)) { continue; } $penalty = $mapPenalty[$item['request_path']] << 1 + ($item['store_id'] ? 0 : 1); if (!$foundItem || $currentPenalty > $penalty) { $foundItem = $item; $currentPenalty = $penalty; if (!$currentPenalty) { // Found best matching item with zero penalty, no reason to continue break; } } } // Set data and finish loading if ($foundItem) { $object->setData($foundItem); } // Finish $this->unserializeFields($object); $this->_afterLoad($object); return $this; }
/** * @magentoDbIsolation enabled */ public function testGetStoreId() { $this->model->setStoreId(10); $this->assertEquals(10, $this->model->getStoreId()); }