88use Magento \Framework \Mview \View \AdditionalColumnsProcessor \DefaultProcessor ;
99use Magento \Framework \Mview \View \ChangelogBatchWalker ;
1010use Magento \Framework \Mview \View \SubscriptionInterface ;
11+ use Magento \Framework \App \ResourceConnection ;
1112
1213class Converter implements \Magento \Framework \Config \ConverterInterface
1314{
@@ -22,13 +23,21 @@ class Converter implements \Magento\Framework\Config\ConverterInterface
2223 private $ defaultIterator ;
2324
2425 /**
26+ * @var ResourceConnection
27+ */
28+ private $ resourceConnection ;
29+
30+ /**
31+ * @param ResourceConnection $resourceConnection
2532 * @param string $defaultProcessor
2633 * @param string $defaultIterator
2734 */
2835 public function __construct (
36+ ResourceConnection $ resourceConnection ,
2937 string $ defaultProcessor = DefaultProcessor::class,
3038 string $ defaultIterator = ChangelogBatchWalker::class
3139 ) {
40+ $ this ->resourceConnection = $ resourceConnection ;
3241 $ this ->defaultProcessor = $ defaultProcessor ;
3342 $ this ->defaultIterator = $ defaultIterator ;
3443 }
@@ -100,6 +109,7 @@ protected function convertChild(\DOMNode $childNode, $data)
100109 }
101110 $ name = $ this ->getAttributeValue ($ subscription , 'name ' );
102111 $ column = $ this ->getAttributeValue ($ subscription , 'entity_column ' );
112+ $ column = $ this ->checkColumnOrReturnPrimarykeyColumn ($ name , $ column );
103113 $ subscriptionModel = $ this ->getAttributeValue ($ subscription , 'subscription_model ' );
104114
105115 if (!empty ($ subscriptionModel )
@@ -155,4 +165,32 @@ private function getAdditionalColumns(\DOMNode $subscription): array
155165
156166 return $ additionalColumns ;
157167 }
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+ }
158196}
0 commit comments