2
2
3
3
namespace Algolia \AlgoliaSearch \Service \Product ;
4
4
5
- use Magento \Framework \DB \Select ;
6
5
use Magento \Catalog \Model \ResourceModel \Product \Collection as ProductCollection ;
7
6
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
8
- use Magento \Framework \Indexer \IndexerRegistry ;
7
+ use Magento \Framework \App \ResourceConnection ;
8
+ use Magento \Framework \DB \Select ;
9
9
use Magento \Framework \Indexer \IndexerInterface ;
10
+ use Magento \Framework \Indexer \IndexerRegistry ;
11
+ use Magento \Framework \Indexer \StateInterface ;
10
12
use Zend_Db_Select ;
11
13
12
14
class MissingPriceIndexHandler
@@ -18,6 +20,7 @@ class MissingPriceIndexHandler
18
20
protected IndexerInterface $ indexer ;
19
21
public function __construct (
20
22
protected CollectionFactory $ productCollectionFactory ,
23
+ protected ResourceConnection $ resourceConnection ,
21
24
IndexerRegistry $ indexerRegistry
22
25
)
23
26
{
@@ -27,6 +30,7 @@ public function __construct(
27
30
/**
28
31
* @param string[]|ProductCollection $products
29
32
* @return string[] Array of product IDs that were reindexed by this repair operation
33
+ * @throws \Zend_Db_Select_Exception
30
34
*/
31
35
public function refreshPriceIndex (array |ProductCollection $ products ): array
32
36
{
@@ -43,15 +47,20 @@ public function refreshPriceIndex(array|ProductCollection $products): array
43
47
/**
44
48
* @param string[]|ProductCollection $products
45
49
* @return string[]
50
+ * @throws \Zend_Db_Select_Exception
46
51
*/
47
52
protected function getProductIdsToReindex (array |ProductCollection $ products ): array
48
53
{
49
54
$ productIds = $ products instanceof ProductCollection
50
- ? $ this ->getExpandedProductCollectionIds ($ products )
55
+ ? $ this ->getProductIdsFromCollection ($ products )
51
56
: $ products ;
52
57
58
+ if (empty ($ productIds )) {
59
+ return [];
60
+ }
61
+
53
62
$ state = $ this ->indexer ->getState ()->getStatus ();
54
- if ($ state === \ Magento \ Framework \ Indexer \ StateInterface::STATUS_INVALID ) {
63
+ if ($ state === StateInterface::STATUS_INVALID ) {
55
64
return $ productIds ;
56
65
}
57
66
@@ -77,7 +86,38 @@ protected function filterProductIds(array $productIds): array
77
86
return $ collection ->getAllIds ();
78
87
}
79
88
80
- protected function getExpandedProductCollectionIds (ProductCollection $ collection ): array
89
+ /**
90
+ * Expand the query for product ids from the collection regardless of price index status
91
+ * @throws \Zend_Db_Select_Exception
92
+ * @return string[] An array of indices to be evaluated - array will be empty if no price index join found
93
+ */
94
+ protected function getProductIdsFromCollection (ProductCollection $ collection ): array
95
+ {
96
+
97
+ $ select = clone $ collection ->getSelect ();
98
+ // TODO: Log this exception - pending DiagnosticsLogger
99
+ $ joins = $ select ->getPart (Zend_Db_Select::FROM );
100
+ $ priceIndexJoin = $ this ->getPriceIndexJoinAlias ($ joins );
101
+
102
+ if (!$ priceIndexJoin ) {
103
+ // no price index on query - keep calm and carry on
104
+ return [];
105
+ }
106
+
107
+ $ this ->expandPricingJoin ($ joins , $ priceIndexJoin );
108
+ $ this ->rebuildJoins ($ select , $ joins );
109
+
110
+ return $ this ->resourceConnection ->getConnection ()->fetchCol ($ select );
111
+ }
112
+
113
+ protected function expandPricingJoin (array &$ joins , string $ priceIndexJoin ): void
114
+ {
115
+ $ modifyJoin = &$ joins [$ priceIndexJoin ];
116
+ $ modifyJoin ['joinType ' ] = Zend_Db_Select::LEFT_JOIN ;
117
+ }
118
+
119
+ /** Collection based approach - more memory intensive */
120
+ protected function getProductIdsFromCollectionOrig (ProductCollection $ collection ): array
81
121
{
82
122
$ expandedCollection = clone $ collection ;
83
123
@@ -102,10 +142,14 @@ protected function getExpandedProductCollectionIds(ProductCollection $collection
102
142
103
143
protected function rebuildJoins (Select $ select , array $ joins ): void
104
144
{
145
+ $ select ->reset (Zend_Db_Select::COLUMNS );
105
146
$ select ->reset (Zend_Db_Select::FROM );
106
147
foreach ($ joins as $ alias => $ joinData ) {
107
148
if ($ joinData ['joinType ' ] === Zend_Db_Select::FROM ) {
108
- $ select ->from ([$ alias => $ joinData ['tableName ' ]]);
149
+ $ select ->from (
150
+ [$ alias => $ joinData ['tableName ' ]],
151
+ 'entity_id '
152
+ );
109
153
} elseif ($ joinData ['joinType ' ] === Zend_Db_Select::LEFT_JOIN ) {
110
154
$ select ->joinLeft (
111
155
[$ alias => $ joinData ['tableName ' ]],
@@ -121,7 +165,6 @@ protected function rebuildJoins(Select $select, array $joins): void
121
165
$ joinData ['schema ' ]
122
166
);
123
167
}
124
- $ sql = $ select ->__toString ();
125
168
}
126
169
}
127
170
0 commit comments