@@ -161,41 +161,51 @@ private function deleteOldUrls(array $urls): void
161
161
$ oldUrlsSelect ->from (
162
162
$ this ->resource ->getTableName (self ::TABLE_NAME )
163
163
);
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 = [];
164
198
/** @var UrlRewrite $url */
165
199
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 ()] : [];
185
202
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 ;
198
207
}
208
+ return $ uniqueEntities ;
199
209
}
200
210
201
211
/**
0 commit comments