【问题标题】:Magento 2 URL rewrite Issue : URL key already exists for specified storeMagento 2 URL重写问题:指定商店的URL密钥已经存在
【发布时间】:2019-01-25 11:48:06
【问题描述】:

我在 Magento 2.2.5 上以编程方式保存产品时遇到了这个问题

在任何模块中,如果我在多个产品的循环内执行 $product->save();$this->productRepository->save($product);。我明白了:

PDOException: SQLSTATE[23000]: 完整性约束违规:1062 /home/dev3/www/vendor/magento/zendframework1/library/ 中的键 'URL_REWRITE_REQUEST_PATH_STORE_ID' 的重复条目 'the-lipstick.html-1' Zend/Db/Statement/Pdo.php:228

错误与此处描述的错误类似:https://www.human-element.com/url-key-specified-store-already-exists-magento-2/

使用管理区域登录可以很好地保存产品。

到目前为止,任何建议的修复程序,包括修改核心文件 (DBStorage.php) 的修复程序都不适用于 2.2.5。

到目前为止我尝试了什么: 1.修复https://www.human-element.com/url-key-specified-store-already-exists-magento-2/ 2. 修复https://magento.stackexchange.com/questions/210359/magento-2-product-url-rewrite-issue

请为 M 2.2.5 提出解决方案/修复建议

【问题讨论】:

  • 我们发现 url_key 在最初保存时包含大写字符可能会导致此问题。 Magento 以全小写形式显示url_key,但数据库具有大写字符。 Magento 然后认为重写不存在并尝试使用不同的大小写再次添加它。

标签: php magento magento2


【解决方案1】:

我的修复: 在 di.xml -

<preference for="Magento\UrlRewrite\Model\Storage\DbStorage" type="MyCompany\FixUrls\Model\ProductUrlFix" />

在 ProductFixUrl 中写这两个函数:

protected function doReplace(array $urls){

    $this->deleteOld($urls);
        $data = [];
        $storeId_requestPaths = [];

        foreach ($urls as $url) {
            $storeId = $url->getStoreId();
            $requestPath = $url->getRequestPath();
            // Skip if is exist in the database
            $sql = "SELECT * FROM url_rewrite where store_id = $storeId and request_path = '$requestPath'";
            $exists = $this->connection->fetchOne($sql);

            if ($exists) {
                continue;
            }

            $storeId_requestPaths[] = $storeId . '-' . $requestPath;
            $data[] = $url->toArray();
        }
        try {

            $n = count($storeId_requestPaths);
            for ($i = 0; $i < $n - 1; $i++) {
                for ($j = $i + 1; $j < $n; $j++) {
                    if ($storeId_requestPaths[$i] == $storeId_requestPaths[$j]) {
                        unset($data[$j]);
                    }
                }
            }
            parent::insertMultiple($data);

        } catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
            /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urlConflicted */
            $urlConflicted = [];
            foreach ($urls as $url) {
                $urlFound = parent::doFindOneByData(
                    [
                        UrlRewriteData::REQUEST_PATH => $url->getRequestPath(),
                        UrlRewriteData::STORE_ID => $url->getStoreId(),
                    ]
                );
                if (isset($urlFound[UrlRewriteData::URL_REWRITE_ID])) {
                    $urlConflicted[$urlFound[UrlRewriteData::URL_REWRITE_ID]] = $url->toArray();
                }
            }
            if ($urlConflicted) {
                throw new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException(
                    __('URL key for specified store already exists.'),
                    $e,
                    $e->getCode(),
                    $urlConflicted
                );
            } else {
                throw $e->getPrevious() ?: $e;
            }
        }

        return $urls;
    }

    /**
     * @param UrlRewrite[] $urls
     *
     * @return void
     */
    public function deleteOld(array $urls)
    {
        $oldUrlsSelect = $this->connection->select();
        $oldUrlsSelect->from(
            $this->resource->getTableName(self::TABLE_NAME)
        );
        /** @var UrlRewrite $url */
        foreach ($urls as $url) {
            $oldUrlsSelect->orWhere(
                $this->connection->quoteIdentifier(
                    UrlRewrite::ENTITY_TYPE
                ) . ' = ?',
                $url->getEntityType()
            );
            $oldUrlsSelect->where(
                $this->connection->quoteIdentifier(
                    UrlRewrite::ENTITY_ID
                ) . ' = ?',
                $url->getEntityId()
            );
            $oldUrlsSelect->where(
                $this->connection->quoteIdentifier(
                    UrlRewrite::STORE_ID
                ) . ' = ?',
                $url->getStoreId()
            );
        }

        // prevent query locking in a case when nothing to delete
        $checkOldUrlsSelect = clone $oldUrlsSelect;
        $checkOldUrlsSelect->reset(Select::COLUMNS);
        $checkOldUrlsSelect->columns('count(*)');
        $hasOldUrls = (bool) $this->connection->fetchOne($checkOldUrlsSelect);

        if ($hasOldUrls) {
            $this->connection->query(
                $oldUrlsSelect->deleteFromSelect(
                    $this->resource->getTableName(self::TABLE_NAME)
                )
            );
        }
    }

【讨论】:

  • 我已应用此解决方案并修复了类别保存错误。但是新更新的类别 url 键没有在前面更新任何想法然后请在此处更新?
【解决方案2】:

在迁移和研究问题一周后,唯一对我有用的是https://www.safemage.com/url-optimization-after-migration-magento-2.html

我必须降级到 2.2.7 才能使用它。它说它适用于 2.3,但它没有。

【讨论】:

    【解决方案3】:

    在互联网上查找了几天后,我找不到确切的解决方案。 然后我发现如果我们更改类别的 URLKEY 它不会显示此错误所以我已经这样做了。

    $category->setPath($parentCategory->getPath())
                ->setParentId($parentId)
                ->setName('test1')
                ->setIsActive(true)
                ->setUrlKey(rand(1,1000000000));
    
            $category->save();
    

    我使用随机函数在数据库中添加类别随机没有使用 ->setUrlKey(rand(1,1000000000)); 你可以像这样添加任何东西重复的类别名称和一些随机的 no 等。 如果它可以帮助您放弃,那么错误就会消失。谢谢

    【讨论】:

    • 您好 Ghulam.M,您在哪里进行了此项更改?在类别页面中保存将产品分配给类别时,我遇到了类似的错误,我相信这可能是答案。发现唯一约束违规 {"exception":"[object] (Magento\Framework\Exception\AlreadyExistsException(code: 0): 在 /chroot/home/a60ed167/soupandsaladbars.com/vendor/magento/framework/EntityManager 发现唯一约束违规/Operation/Update.php:121,Magento\Framework\DB\Adapter\DuplicateException(代码:1062):SQLSTATE[23000]:违反完整性约束:1062 重复条目
    • 这是确切的错误:techutils.in/blog/tag/serializer 存在完整性约束违规,随后出现此异常:传递给 Magento\Catalog\Model\Category\FileInfo::removeStorePath() 的参数 1 必须是类型字符串,给定数组,在第 167 行的 /vendor/magento/module-catalog/Model/Category/FileInfo.php 中调用
    • 嗨@ComputerGiant 我刚才在创建一个类别时这样做了......对于你的错误我建议你在magento.stackexchange.com上发布一个问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 2015-08-25
    • 2017-09-07
    • 2016-06-01
    相关资源
    最近更新 更多