@@ -222,30 +222,26 @@ private function deleteOldUrls(array $urls): void
222
222
$ oldUrlsSelect ->from (
223
223
$ this ->resource ->getTableName (self ::TABLE_NAME )
224
224
);
225
-
226
225
$ uniqueEntities = $ this ->prepareUniqueEntities ($ urls );
227
226
foreach ($ uniqueEntities as $ storeId => $ entityTypes ) {
228
227
foreach ($ entityTypes as $ entityType => $ entities ) {
229
228
$ oldUrlsSelect ->orWhere (
230
- $ this ->connection ->quoteIdentifier (
231
- UrlRewrite::STORE_ID
232
- ) . ' = ' . $ this ->connection ->quote ($ storeId , 'INTEGER ' ) .
233
- ' AND ' . $ this ->connection ->quoteIdentifier (
234
- UrlRewrite::ENTITY_ID
235
- ) . ' IN ( ' . $ this ->connection ->quote ($ entities , 'INTEGER ' ) . ') ' .
236
- ' AND ' . $ this ->connection ->quoteIdentifier (
237
- UrlRewrite::ENTITY_TYPE
238
- ) . ' = ' . $ this ->connection ->quote ($ entityType )
229
+ $ this ->connection ->quoteIdentifier (UrlRewrite::STORE_ID )
230
+ . ' = ' . $ this ->connection ->quote ($ storeId , 'INTEGER ' ) .
231
+ ' AND ' . $ this ->connection ->quoteIdentifier (UrlRewrite::ENTITY_ID )
232
+ . ' IN ( ' . $ this ->connection ->quote (array_keys ($ entities ), 'INTEGER ' ) . ') ' .
233
+ ' AND ' . $ this ->connection ->quoteIdentifier (UrlRewrite::ENTITY_TYPE )
234
+ . ' = ' . $ this ->connection ->quote ($ entityType ) .
235
+ ' AND ' . $ this ->connection ->quoteIdentifier (UrlRewrite::REQUEST_PATH )
236
+ . ' NOT IN ( ' . $ this ->connection ->quote (array_merge (...$ entities )) . ') '
239
237
);
240
238
}
241
239
}
242
-
243
240
// prevent query locking in a case when nothing to delete
244
241
$ checkOldUrlsSelect = clone $ oldUrlsSelect ;
245
242
$ checkOldUrlsSelect ->reset (Select::COLUMNS );
246
243
$ checkOldUrlsSelect ->columns ('count(*) ' );
247
244
$ hasOldUrls = (bool )$ this ->connection ->fetchOne ($ checkOldUrlsSelect );
248
-
249
245
if ($ hasOldUrls ) {
250
246
$ this ->connection ->query (
251
247
$ oldUrlsSelect ->deleteFromSelect (
@@ -264,17 +260,9 @@ private function deleteOldUrls(array $urls): void
264
260
private function prepareUniqueEntities (array $ urls ): array
265
261
{
266
262
$ uniqueEntities = [];
267
- /** @var UrlRewrite $url */
268
263
foreach ($ urls as $ url ) {
269
- $ entityIds = (!empty ($ uniqueEntities [$ url ->getStoreId ()][$ url ->getEntityType ()])) ?
270
- $ uniqueEntities [$ url ->getStoreId ()][$ url ->getEntityType ()] : [];
271
-
272
- if (!\in_array ($ url ->getEntityId (), $ entityIds )) {
273
- $ entityIds [] = $ url ->getEntityId ();
274
- }
275
- $ uniqueEntities [$ url ->getStoreId ()][$ url ->getEntityType ()] = $ entityIds ;
264
+ $ uniqueEntities [$ url ->getStoreId ()][$ url ->getEntityType ()][$ url ->getEntityId ()][] = $ url ->getRequestPath ();
276
265
}
277
-
278
266
return $ uniqueEntities ;
279
267
}
280
268
@@ -291,7 +279,7 @@ protected function doReplace(array $urls): array
291
279
foreach ($ urls as $ url ) {
292
280
$ data [] = $ url ->toArray ();
293
281
}
294
- $ this ->insertMultiple ($ data );
282
+ $ this ->upsertMultiple ($ data );
295
283
$ this ->connection ->commit ();
296
284
} catch (\Magento \Framework \DB \Adapter \DeadlockException $ deadlockException ) {
297
285
continue ;
@@ -336,6 +324,8 @@ protected function doReplace(array $urls): array
336
324
* @return void
337
325
* @throws \Magento\Framework\Exception\AlreadyExistsException|\Exception
338
326
* @throws \Exception
327
+ * @deprecated Not used anymore.
328
+ * @see upsertMultiple
339
329
*/
340
330
protected function insertMultiple ($ data ): void
341
331
{
@@ -354,6 +344,17 @@ protected function insertMultiple($data): void
354
344
}
355
345
}
356
346
347
+ /**
348
+ * Upsert multiple
349
+ *
350
+ * @param array $data
351
+ * @return void
352
+ */
353
+ private function upsertMultiple (array $ data ): void
354
+ {
355
+ $ this ->connection ->insertOnDuplicate ($ this ->resource ->getTableName (self ::TABLE_NAME ), $ data );
356
+ }
357
+
357
358
/**
358
359
* Get filter for url rows deletion due to provided urls
359
360
*
0 commit comments