5
5
*/
6
6
namespace Magento \Catalog \Model \Indexer \Product \Flat \Action ;
7
7
8
+ use Magento \Catalog \Api \Data \ProductInterface ;
8
9
use Magento \Catalog \Model \Indexer \Product \Flat \FlatTableBuilder ;
9
10
use Magento \Catalog \Model \Indexer \Product \Flat \TableBuilder ;
11
+ use Magento \Framework \EntityManager \MetadataPool ;
10
12
11
13
/**
12
- * Class Row reindex action
14
+ * Class Row reindex action.
15
+ *
16
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
13
17
*/
14
18
class Row extends \Magento \Catalog \Model \Indexer \Product \Flat \AbstractAction
15
19
{
16
20
/**
17
- * @var \Magento\Catalog\Model\Indexer\Product\Flat\Action\ Indexer
21
+ * @var Indexer
18
22
*/
19
23
protected $ flatItemWriter ;
20
24
@@ -23,6 +27,11 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
23
27
*/
24
28
protected $ flatItemEraser ;
25
29
30
+ /**
31
+ * @var MetadataPool
32
+ */
33
+ private $ metadataPool ;
34
+
26
35
/**
27
36
* @param \Magento\Framework\App\ResourceConnection $resource
28
37
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
@@ -32,6 +41,7 @@ class Row extends \Magento\Catalog\Model\Indexer\Product\Flat\AbstractAction
32
41
* @param FlatTableBuilder $flatTableBuilder
33
42
* @param Indexer $flatItemWriter
34
43
* @param Eraser $flatItemEraser
44
+ * @param MetadataPool|null $metadataPool
35
45
*/
36
46
public function __construct (
37
47
\Magento \Framework \App \ResourceConnection $ resource ,
@@ -41,7 +51,8 @@ public function __construct(
41
51
TableBuilder $ tableBuilder ,
42
52
FlatTableBuilder $ flatTableBuilder ,
43
53
Indexer $ flatItemWriter ,
44
- Eraser $ flatItemEraser
54
+ Eraser $ flatItemEraser ,
55
+ MetadataPool $ metadataPool = null
45
56
) {
46
57
parent ::__construct (
47
58
$ resource ,
@@ -53,6 +64,8 @@ public function __construct(
53
64
);
54
65
$ this ->flatItemWriter = $ flatItemWriter ;
55
66
$ this ->flatItemEraser = $ flatItemEraser ;
67
+ $ this ->metadataPool = $ metadataPool ?:
68
+ \Magento \Framework \App \ObjectManager::getInstance ()->get (MetadataPool::class);
56
69
}
57
70
58
71
/**
@@ -61,7 +74,6 @@ public function __construct(
61
74
* @param int|null $id
62
75
* @return \Magento\Catalog\Model\Indexer\Product\Flat\Action\Row
63
76
* @throws \Magento\Framework\Exception\LocalizedException
64
- * @throws \Zend_Db_Statement_Exception
65
77
*/
66
78
public function execute ($ id = null )
67
79
{
@@ -71,50 +83,47 @@ public function execute($id = null)
71
83
);
72
84
}
73
85
$ ids = [$ id ];
74
- foreach ($ this ->_storeManager ->getStores () as $ store ) {
86
+ $ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
87
+
88
+ $ stores = $ this ->_storeManager ->getStores ();
89
+ foreach ($ stores as $ store ) {
75
90
$ tableExists = $ this ->_isFlatTableExists ($ store ->getId ());
76
91
if ($ tableExists ) {
77
92
$ this ->flatItemEraser ->removeDeletedProducts ($ ids , $ store ->getId ());
78
93
}
79
94
80
95
/* @var $status \Magento\Eav\Model\Entity\Attribute */
81
- $ status = $ this ->_productIndexerHelper ->getAttribute (' status ' );
96
+ $ status = $ this ->_productIndexerHelper ->getAttribute (ProductInterface:: STATUS );
82
97
$ statusTable = $ status ->getBackend ()->getTable ();
83
98
$ statusConditions = [
84
99
'store_id IN(0, ' . (int )$ store ->getId () . ') ' ,
85
100
'attribute_id = ' . (int )$ status ->getId (),
86
- ' entity_id = ' . (int )$ id
101
+ $ linkField . ' = ' . (int )$ id,
87
102
];
88
103
$ select = $ this ->_connection ->select ();
89
- $ select ->from (
90
- $ statusTable ,
91
- ['value ' ]
92
- )->where (
93
- implode (' AND ' , $ statusConditions )
94
- )->order (
95
- 'store_id DESC '
96
- );
104
+ $ select ->from ($ statusTable , ['value ' ])
105
+ ->where (implode (' AND ' , $ statusConditions ))
106
+ ->order ('store_id DESC ' )
107
+ ->limit (1 );
97
108
$ result = $ this ->_connection ->query ($ select );
98
- $ status = $ result ->fetch ( 1 );
109
+ $ status = $ result ->fetchColumn ( 0 );
99
110
100
- if ($ status ['value ' ] == \Magento \Catalog \Model \Product \Attribute \Source \Status::STATUS_ENABLED ) {
101
- if (isset ($ ids [0 ])) {
102
- if (!$ tableExists ) {
103
- $ this ->_flatTableBuilder ->build (
104
- $ store ->getId (),
105
- [$ ids [0 ]],
106
- $ this ->_valueFieldSuffix ,
107
- $ this ->_tableDropSuffix ,
108
- false
109
- );
110
- }
111
- $ this ->flatItemWriter ->write ($ store ->getId (), $ ids [0 ], $ this ->_valueFieldSuffix );
111
+ if ($ status == \Magento \Catalog \Model \Product \Attribute \Source \Status::STATUS_ENABLED ) {
112
+ if (!$ tableExists ) {
113
+ $ this ->_flatTableBuilder ->build (
114
+ $ store ->getId (),
115
+ $ ids ,
116
+ $ this ->_valueFieldSuffix ,
117
+ $ this ->_tableDropSuffix ,
118
+ false
119
+ );
112
120
}
113
- }
114
- if ( $ status [ ' value ' ] == \ Magento \ Catalog \ Model \ Product \ Attribute \ Source \Status:: STATUS_DISABLED ) {
121
+ $ this -> flatItemWriter -> write ( $ store -> getId (), $ id , $ this -> _valueFieldSuffix );
122
+ } else {
115
123
$ this ->flatItemEraser ->deleteProductsFromStore ($ id , $ store ->getId ());
116
124
}
117
125
}
126
+
118
127
return $ this ;
119
128
}
120
129
}
0 commit comments