5
5
*/
6
6
namespace Magento \CatalogSearch \Model \Indexer \Fulltext \Action ;
7
7
8
+ use Exception ;
8
9
use Magento \Catalog \Api \Data \ProductInterface ;
9
10
use Magento \Catalog \Model \Product ;
10
- use Magento \Catalog \Model \Product \Attribute \Source \Status ;
11
11
use Magento \Catalog \Model \Product \Type ;
12
12
use Magento \Catalog \Model \ResourceModel \Product \Attribute \CollectionFactory ;
13
13
use Magento \CatalogSearch \Model \ResourceModel \EngineInterface ;
14
14
use Magento \CatalogSearch \Model \ResourceModel \EngineProvider ;
15
15
use Magento \Eav \Model \Config ;
16
16
use Magento \Eav \Model \Entity \Attribute ;
17
+ use Magento \Framework \App \ObjectManager ;
17
18
use Magento \Framework \App \ResourceConnection ;
18
19
use Magento \Framework \DataObject ;
19
20
use Magento \Framework \DB \Adapter \AdapterInterface ;
20
21
use Magento \Framework \DB \Select ;
21
22
use Magento \Framework \EntityManager \EntityMetadata ;
22
23
use Magento \Framework \EntityManager \MetadataPool ;
23
24
use Magento \Framework \Event \ManagerInterface ;
24
- use Magento \Store \Model \Store ;
25
25
use Magento \Store \Model \StoreManagerInterface ;
26
26
use Zend_Db ;
27
27
@@ -138,6 +138,11 @@ class DataProvider
138
138
*/
139
139
private $ antiGapMultiplier ;
140
140
141
+ /**
142
+ * @var GetSearchableProductsSelect|mixed
143
+ */
144
+ private $ selectSearchableProducts ;
145
+
141
146
/**
142
147
* @param ResourceConnection $resource
143
148
* @param Type $catalogProductType
@@ -148,6 +153,8 @@ class DataProvider
148
153
* @param StoreManagerInterface $storeManager
149
154
* @param MetadataPool $metadataPool
150
155
* @param int $antiGapMultiplier
156
+ * @param GetSearchableProductsSelect|null $getSearchableProductsSelect
157
+ * @throws Exception
151
158
*/
152
159
public function __construct (
153
160
ResourceConnection $ resource ,
@@ -158,7 +165,8 @@ public function __construct(
158
165
ManagerInterface $ eventManager ,
159
166
StoreManagerInterface $ storeManager ,
160
167
MetadataPool $ metadataPool ,
161
- int $ antiGapMultiplier = 5
168
+ int $ antiGapMultiplier = 5 ,
169
+ GetSearchableProductsSelect $ getSearchableProductsSelect = null
162
170
) {
163
171
$ this ->resource = $ resource ;
164
172
$ this ->connection = $ resource ->getConnection ();
@@ -170,6 +178,7 @@ public function __construct(
170
178
$ this ->engine = $ engineProvider ->get ();
171
179
$ this ->metadata = $ metadataPool ->getMetadata (ProductInterface::class);
172
180
$ this ->antiGapMultiplier = $ antiGapMultiplier ;
181
+ $ this ->selectSearchableProducts = $ getSearchableProductsSelect ?: ObjectManager::getInstance ()->get (GetSearchableProductsSelect::class);
173
182
}
174
183
175
184
/**
@@ -201,8 +210,8 @@ public function getSearchableProducts(
201
210
$ lastProductId = 0 ,
202
211
$ batch = 100
203
212
) {
204
- // $batch = 10;
205
- $ select = $ this ->getSelectForSearchableProducts ($ storeId , $ staticFields , $ productIds , $ lastProductId , $ batch );
213
+ $ batch = 10 ;
214
+ $ select = $ this ->selectSearchableProducts -> execute ($ storeId , $ staticFields , $ productIds , $ lastProductId , $ batch );
206
215
if ($ productIds === null ) {
207
216
$ select ->where (
208
217
'e.entity_id < ? ' ,
@@ -213,106 +222,13 @@ public function getSearchableProducts(
213
222
if ($ productIds === null && !$ products ) {
214
223
// try to search without limit entity_id by batch size for cover case with a big gap between entity ids
215
224
$ products = $ this ->connection ->fetchAll (
216
- $ this ->getSelectForSearchableProducts ($ storeId , $ staticFields , $ productIds , $ lastProductId , $ batch )
225
+ $ this ->selectSearchableProducts -> execute ($ storeId , $ staticFields , $ productIds , $ lastProductId , $ batch )
217
226
);
218
227
}
219
228
220
229
return $ products ;
221
230
}
222
231
223
- /**
224
- * Get Select object for searchable products
225
- *
226
- * @param int $storeId
227
- * @param array $staticFields
228
- * @param array|int $productIds
229
- * @param int $lastProductId
230
- * @param int $batch
231
- * @return Select
232
- */
233
- public function getSelectForSearchableProducts (
234
- $ storeId ,
235
- array $ staticFields ,
236
- $ productIds ,
237
- $ lastProductId ,
238
- $ batch
239
- ) {
240
- $ websiteId = (int )$ this ->storeManager ->getStore ($ storeId )->getWebsiteId ();
241
- $ lastProductId = (int )$ lastProductId ;
242
-
243
- $ select = $ this ->connection ->select ()
244
- ->useStraightJoin (true )
245
- ->from (
246
- ['e ' => $ this ->getTable ('catalog_product_entity ' )],
247
- array_merge (['entity_id ' , 'type_id ' ], $ staticFields )
248
- )
249
- ->join (
250
- ['website ' => $ this ->getTable ('catalog_product_website ' )],
251
- $ this ->connection ->quoteInto ('website.product_id = e.entity_id AND website.website_id = ? ' , $ websiteId ),
252
- []
253
- );
254
-
255
- $ this ->joinAttribute ($ select , 'visibility ' , $ storeId , $ this ->engine ->getAllowedVisibility ());
256
- $ this ->joinAttribute ($ select , 'status ' , $ storeId , [Status::STATUS_ENABLED ]);
257
-
258
- if ($ productIds !== null ) {
259
- $ select ->where ('e.entity_id IN (?) ' , $ productIds , Zend_Db::INT_TYPE );
260
- }
261
- $ select ->where ('e.entity_id > ? ' , $ lastProductId );
262
- $ select ->order ('e.entity_id ' );
263
- $ select ->limit ($ batch );
264
-
265
- return $ select ;
266
- }
267
-
268
- /**
269
- * Join attribute to searchable product for filtration
270
- *
271
- * @param Select $select
272
- * @param string $attributeCode
273
- * @param int $storeId
274
- * @param array $whereValue
275
- */
276
- private function joinAttribute (Select $ select , $ attributeCode , $ storeId , array $ whereValue )
277
- {
278
- $ linkField = $ this ->metadata ->getLinkField ();
279
- $ attribute = $ this ->getSearchableAttribute ($ attributeCode );
280
- $ attributeTable = $ this ->getTable ('catalog_product_entity_ ' . $ attribute ->getBackendType ());
281
- $ defaultAlias = $ attributeCode . '_default ' ;
282
- $ storeAlias = $ attributeCode . '_store ' ;
283
-
284
- $ whereCondition = $ this ->connection ->getCheckSql (
285
- $ storeAlias . '.value_id > 0 ' ,
286
- $ storeAlias . '.value ' ,
287
- $ defaultAlias . '.value '
288
- );
289
-
290
- $ select ->join (
291
- [$ defaultAlias => $ attributeTable ],
292
- $ this ->connection ->quoteInto (
293
- $ defaultAlias . '. ' . $ linkField . '= e. ' . $ linkField . ' AND ' . $ defaultAlias . '.attribute_id = ? ' ,
294
- $ attribute ->getAttributeId ()
295
- ) . $ this ->connection ->quoteInto (
296
- ' AND ' . $ defaultAlias . '.store_id = ? ' ,
297
- Store::DEFAULT_STORE_ID
298
- ),
299
- []
300
- )->joinLeft (
301
- [$ storeAlias => $ attributeTable ],
302
- $ this ->connection ->quoteInto (
303
- $ storeAlias . '. ' . $ linkField . '= e. ' . $ linkField . ' AND ' . $ storeAlias . '.attribute_id = ? ' ,
304
- $ attribute ->getAttributeId ()
305
- ) . $ this ->connection ->quoteInto (
306
- ' AND ' . $ storeAlias . '.store_id = ? ' ,
307
- $ storeId
308
- ),
309
- []
310
- )->where (
311
- $ whereCondition . ' IN (?) ' ,
312
- $ whereValue
313
- );
314
- }
315
-
316
232
/**
317
233
* Retrieve searchable attributes
318
234
*
0 commit comments