Skip to content

Commit 398ae00

Browse files
author
Oleksandr Iegorov
committed
MAGETWO-97210: Add Product by SKU to category causes the browser to hang
1 parent ba7fc81 commit 398ae00

File tree

1 file changed

+41
-31
lines changed

1 file changed

+41
-31
lines changed

app/code/Magento/UrlRewrite/Model/Storage/DbStorage.php

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -161,41 +161,51 @@ private function deleteOldUrls(array $urls): void
161161
$oldUrlsSelect->from(
162162
$this->resource->getTableName(self::TABLE_NAME)
163163
);
164+
165+
$uniqueEntities = $this->prepareUniqueEntities($urls);
166+
foreach ($uniqueEntities as $storeId => $entityTypes) {
167+
foreach ($entityTypes as $entityType => $entities) {
168+
$oldUrlsSelect->orWhere(
169+
$this->connection->quoteIdentifier(
170+
UrlRewrite::STORE_ID
171+
) . ' = ' . $this->connection->quote($storeId, 'INTEGER') .
172+
' AND ' . $this->connection->quoteIdentifier(
173+
UrlRewrite::ENTITY_ID
174+
) . ' IN (' . $this->connection->quote($entities, 'INTEGER') . ')' .
175+
' AND ' . $this->connection->quoteIdentifier(
176+
UrlRewrite::ENTITY_TYPE
177+
) . ' = ' . $this->connection->quote($entityType)
178+
);
179+
}
180+
}
181+
182+
$this->connection->query(
183+
$oldUrlsSelect->deleteFromSelect(
184+
$this->resource->getTableName(self::TABLE_NAME)
185+
)
186+
);
187+
}
188+
189+
/**
190+
* Prepare array with unique entities
191+
*
192+
* @param UrlRewrite[] $urls
193+
* @return array
194+
*/
195+
private function prepareUniqueEntities(array $urls): array
196+
{
197+
$uniqueEntities = [];
164198
/** @var UrlRewrite $url */
165199
foreach ($urls as $url) {
166-
$oldUrlsSelect->orWhere(
167-
$this->connection->quoteIdentifier(
168-
UrlRewrite::ENTITY_TYPE
169-
) . ' = ?',
170-
$url->getEntityType()
171-
);
172-
$oldUrlsSelect->where(
173-
$this->connection->quoteIdentifier(
174-
UrlRewrite::ENTITY_ID
175-
) . ' = ?',
176-
$url->getEntityId()
177-
);
178-
$oldUrlsSelect->where(
179-
$this->connection->quoteIdentifier(
180-
UrlRewrite::STORE_ID
181-
) . ' = ?',
182-
$url->getStoreId()
183-
);
184-
}
200+
$entityIds = (!empty($uniqueEntities[$url->getStoreId()][$url->getEntityType()])) ?
201+
$uniqueEntities[$url->getStoreId()][$url->getEntityType()] : [];
185202

186-
// prevent query locking in a case when nothing to delete
187-
$checkOldUrlsSelect = clone $oldUrlsSelect;
188-
$checkOldUrlsSelect->reset(Select::COLUMNS);
189-
$checkOldUrlsSelect->columns('count(*)');
190-
$hasOldUrls = (bool)$this->connection->fetchOne($checkOldUrlsSelect);
191-
192-
if ($hasOldUrls) {
193-
$this->connection->query(
194-
$oldUrlsSelect->deleteFromSelect(
195-
$this->resource->getTableName(self::TABLE_NAME)
196-
)
197-
);
203+
if (!\in_array($url->getEntityId(), $entityIds)) {
204+
$entityIds[] = $url->getEntityId();
205+
}
206+
$uniqueEntities[$url->getStoreId()][$url->getEntityType()] = $entityIds;
198207
}
208+
return $uniqueEntities;
199209
}
200210

201211
/**

0 commit comments

Comments
 (0)