8
8
use Magento \Framework \Mview \View \AdditionalColumnsProcessor \DefaultProcessor ;
9
9
use Magento \Framework \Mview \View \ChangelogBatchWalker ;
10
10
use Magento \Framework \Mview \View \SubscriptionInterface ;
11
+ use Magento \Framework \App \ResourceConnection ;
11
12
12
13
class Converter implements \Magento \Framework \Config \ConverterInterface
13
14
{
@@ -22,13 +23,21 @@ class Converter implements \Magento\Framework\Config\ConverterInterface
22
23
private $ defaultIterator ;
23
24
24
25
/**
26
+ * @var ResourceConnection
27
+ */
28
+ private $ resourceConnection ;
29
+
30
+ /**
31
+ * @param ResourceConnection $resourceConnection
25
32
* @param string $defaultProcessor
26
33
* @param string $defaultIterator
27
34
*/
28
35
public function __construct (
36
+ ResourceConnection $ resourceConnection ,
29
37
string $ defaultProcessor = DefaultProcessor::class,
30
38
string $ defaultIterator = ChangelogBatchWalker::class
31
39
) {
40
+ $ this ->resourceConnection = $ resourceConnection ;
32
41
$ this ->defaultProcessor = $ defaultProcessor ;
33
42
$ this ->defaultIterator = $ defaultIterator ;
34
43
}
@@ -100,6 +109,7 @@ protected function convertChild(\DOMNode $childNode, $data)
100
109
}
101
110
$ name = $ this ->getAttributeValue ($ subscription , 'name ' );
102
111
$ column = $ this ->getAttributeValue ($ subscription , 'entity_column ' );
112
+ $ column = $ this ->checkColumnOrReturnPrimarykeyColumn ($ name , $ column );
103
113
$ subscriptionModel = $ this ->getAttributeValue ($ subscription , 'subscription_model ' );
104
114
105
115
if (!empty ($ subscriptionModel )
@@ -155,4 +165,32 @@ private function getAdditionalColumns(\DOMNode $subscription): array
155
165
156
166
return $ additionalColumns ;
157
167
}
168
+
169
+ /**
170
+ * Check if column exists in table, otherwise return primary key column
171
+ *
172
+ * @param string $tableName
173
+ * @param string $columnName
174
+ * @return string
175
+ */
176
+ private function checkColumnOrReturnPrimarykeyColumn ($ tableName , $ columnName )
177
+ {
178
+ $ connection = $ this ->resourceConnection ->getConnection ();
179
+ $ tableName = $ this ->resourceConnection ->getTableName ($ tableName );
180
+
181
+ if (!$ connection ->isTableExists ($ tableName ) || $ connection ->tableColumnExists ($ tableName , $ columnName )) {
182
+ return $ columnName ;
183
+ }
184
+
185
+ $ primarykeyColumn = null ;
186
+ $ columns = $ connection ->describeTable ($ tableName );
187
+ foreach ($ columns as $ column ) {
188
+ if (!empty ($ column ['PRIMARY ' ])) {
189
+ $ primarykeyColumn = $ column ['COLUMN_NAME ' ];
190
+ break ;
191
+ }
192
+ }
193
+
194
+ return $ primarykeyColumn ;
195
+ }
158
196
}
0 commit comments