8
8
9
9
use Magento \Framework \App \ObjectManager ;
10
10
use Magento \Framework \App \ResourceConnection ;
11
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
11
12
use Magento \Framework \DB \Ddl \Trigger ;
13
+ use Magento \Framework \DB \Ddl \TriggerFactory ;
12
14
use Magento \Framework \Mview \Config ;
13
- use Magento \Framework \Mview \View \ StateInterface ;
15
+ use Magento \Framework \Mview \ViewInterface ;
14
16
15
17
/**
16
- * Class Subscription for handling partial indexation triggers
18
+ * Mview subscription.
17
19
*/
18
20
class Subscription implements SubscriptionInterface
19
21
{
20
22
/**
21
23
* Database connection
22
24
*
23
- * @var \Magento\Framework\DB\Adapter\ AdapterInterface
25
+ * @var AdapterInterface
24
26
*/
25
27
protected $ connection ;
26
28
27
29
/**
28
- * @var \Magento\Framework\DB\Ddl\ TriggerFactory
30
+ * @var TriggerFactory
29
31
*/
30
32
protected $ triggerFactory ;
31
33
32
34
/**
33
- * @var \Magento\Framework\Mview\View\ CollectionInterface
35
+ * @var CollectionInterface
34
36
*/
35
37
protected $ viewCollection ;
36
38
@@ -62,7 +64,7 @@ class Subscription implements SubscriptionInterface
62
64
*
63
65
* @var array
64
66
*/
65
- private $ ignoredUpdateColumns = [] ;
67
+ private $ ignoredUpdateColumns ;
66
68
67
69
/**
68
70
* List of columns that can be updated in a specific subscribed table
@@ -74,16 +76,17 @@ class Subscription implements SubscriptionInterface
74
76
* @var Resource
75
77
*/
76
78
protected $ resource ;
79
+
77
80
/**
78
81
* @var Config
79
82
*/
80
83
private $ mviewConfig ;
81
84
82
85
/**
83
86
* @param ResourceConnection $resource
84
- * @param \Magento\Framework\DB\Ddl\ TriggerFactory $triggerFactory
85
- * @param \Magento\Framework\Mview\View\ CollectionInterface $viewCollection
86
- * @param \Magento\Framework\Mview\ ViewInterface $view
87
+ * @param TriggerFactory $triggerFactory
88
+ * @param CollectionInterface $viewCollection
89
+ * @param ViewInterface $view
87
90
* @param string $tableName
88
91
* @param string $columnName
89
92
* @param array $ignoredUpdateColumns
@@ -92,9 +95,9 @@ class Subscription implements SubscriptionInterface
92
95
*/
93
96
public function __construct (
94
97
ResourceConnection $ resource ,
95
- \ Magento \ Framework \ DB \ Ddl \ TriggerFactory $ triggerFactory ,
96
- \ Magento \ Framework \ Mview \ View \ CollectionInterface $ viewCollection ,
97
- \ Magento \ Framework \ Mview \ ViewInterface $ view ,
98
+ TriggerFactory $ triggerFactory ,
99
+ CollectionInterface $ viewCollection ,
100
+ ViewInterface $ view ,
98
101
$ tableName ,
99
102
$ columnName ,
100
103
$ ignoredUpdateColumns = [],
@@ -114,9 +117,9 @@ public function __construct(
114
117
}
115
118
116
119
/**
117
- * Create subsciption
120
+ * Create subscription
118
121
*
119
- * @return \Magento\Framework\Mview\View\ SubscriptionInterface
122
+ * @return SubscriptionInterface
120
123
*/
121
124
public function create ()
122
125
{
@@ -129,12 +132,12 @@ public function create()
129
132
->setEvent ($ event )
130
133
->setTable ($ this ->resource ->getTableName ($ this ->tableName ));
131
134
132
- $ trigger ->addStatement ($ this ->buildStatement ($ event , $ this ->getView ()-> getChangelog () ));
135
+ $ trigger ->addStatement ($ this ->buildStatement ($ event , $ this ->getView ()));
133
136
134
137
// Add statements for linked views
135
138
foreach ($ this ->getLinkedViews () as $ view ) {
136
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
137
- $ trigger ->addStatement ($ this ->buildStatement ($ event , $ view-> getChangelog () ));
139
+ /** @var ViewInterface $view */
140
+ $ trigger ->addStatement ($ this ->buildStatement ($ event , $ view ));
138
141
}
139
142
140
143
$ this ->connection ->dropTrigger ($ trigger ->getName ());
@@ -147,7 +150,7 @@ public function create()
147
150
/**
148
151
* Remove subscription
149
152
*
150
- * @return \Magento\Framework\Mview\View\ SubscriptionInterface
153
+ * @return SubscriptionInterface
151
154
*/
152
155
public function remove ()
153
156
{
@@ -162,8 +165,8 @@ public function remove()
162
165
163
166
// Add statements for linked views
164
167
foreach ($ this ->getLinkedViews () as $ view ) {
165
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
166
- $ trigger ->addStatement ($ this ->buildStatement ($ event , $ view-> getChangelog () ));
168
+ /** @var ViewInterface $view */
169
+ $ trigger ->addStatement ($ this ->buildStatement ($ event , $ view ));
167
170
}
168
171
169
172
$ this ->connection ->dropTrigger ($ trigger ->getName ());
@@ -188,7 +191,7 @@ protected function getLinkedViews()
188
191
$ viewList = $ this ->viewCollection ->getViewsByStateMode (StateInterface::MODE_ENABLED );
189
192
190
193
foreach ($ viewList as $ view ) {
191
- /** @var \Magento\Framework\Mview\ ViewInterface $view */
194
+ /** @var ViewInterface $view */
192
195
// Skip the current view
193
196
if ($ view ->getId () == $ this ->getView ()->getId ()) {
194
197
continue ;
@@ -208,21 +211,22 @@ protected function getLinkedViews()
208
211
/**
209
212
* Prepare columns for trigger statement. Should be protected in order to serve new approach
210
213
*
211
- * @param ChangelogInterface $changelog
214
+ * @param ViewInterface $view
212
215
* @param string $event
213
216
* @return array
214
217
* @throws \Exception
215
218
*/
216
- protected function prepareColumns (ChangelogInterface $ changelog , string $ event ): array
219
+ protected function prepareColumns (ViewInterface $ view , string $ event ): array
217
220
{
221
+ $ changelog = $ view ->getChangelog ();
218
222
$ prefix = $ event === Trigger::EVENT_DELETE ? 'OLD. ' : 'NEW. ' ;
219
223
$ subscriptionData = $ this ->mviewConfig ->getView ($ changelog ->getViewId ())['subscriptions ' ][$ this ->getTableName ()];
220
224
$ columns = [
221
225
'column_names ' => [
222
226
'entity_id ' => $ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ())
223
227
],
224
228
'column_values ' => [
225
- 'entity_id ' => $ this ->getEntityColumn ($ prefix )
229
+ 'entity_id ' => $ this ->getEntityColumn ($ prefix, $ view )
226
230
]
227
231
];
228
232
@@ -241,12 +245,14 @@ protected function prepareColumns(ChangelogInterface $changelog, string $event):
241
245
* Build trigger statement for INSERT, UPDATE, DELETE events
242
246
*
243
247
* @param string $event
244
- * @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
248
+ * @param ViewInterface $view
245
249
* @return string
246
250
*/
247
- protected function buildStatement ($ event , $ changelog )
251
+ protected function buildStatement (string $ event , ViewInterface $ view ): string
248
252
{
249
253
$ trigger = "%sINSERT IGNORE INTO %s (%s) VALUES (%s); " ;
254
+ $ changelog = $ view ->getChangelog ();
255
+
250
256
switch ($ event ) {
251
257
case Trigger::EVENT_UPDATE :
252
258
$ tableName = $ this ->resource ->getTableName ($ this ->getTableName ());
@@ -279,13 +285,14 @@ protected function buildStatement($event, $changelog)
279
285
}
280
286
break ;
281
287
}
282
- $ columns = $ this ->prepareColumns ($ changelog , $ event );
288
+ $ columns = $ this ->prepareColumns ($ view , $ event );
289
+
283
290
return sprintf (
284
291
$ trigger ,
285
292
$ this ->getProcessor ()->getPreStatements (),
286
293
$ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
287
- implode (" , " , $ columns ['column_names ' ]),
288
- implode (" , " , $ columns ['column_values ' ])
294
+ implode (' , ' , $ columns ['column_names ' ]),
295
+ implode (' , ' , $ columns ['column_values ' ])
289
296
);
290
297
}
291
298
@@ -312,11 +319,28 @@ private function getProcessor(): AdditionalColumnProcessorInterface
312
319
313
320
/**
314
321
* @param string $prefix
322
+ * @param ViewInterface $view
323
+ * @return string
324
+ */
325
+ public function getEntityColumn (string $ prefix , ViewInterface $ view ): string
326
+ {
327
+ return $ prefix . $ this ->connection ->quoteIdentifier ($ this ->getSubscriptionColumn ($ view ));
328
+ }
329
+
330
+ /**
331
+ * Returns subscription column name by view
332
+ *
333
+ * @param ViewInterface $view
315
334
* @return string
316
335
*/
317
- public function getEntityColumn ( string $ prefix ): string
336
+ private function getSubscriptionColumn ( ViewInterface $ view ): string
318
337
{
319
- return $ prefix . $ this ->connection ->quoteIdentifier ($ this ->getColumnName ());
338
+ $ subscriptions = $ view ->getSubscriptions ();
339
+ if (!isset ($ subscriptions [$ this ->getTableName ()]['column ' ])) {
340
+ throw new \RuntimeException (sprintf ('Column name for view with id "%s" doesn \'t exist ' , $ view ->getId ()));
341
+ }
342
+
343
+ return $ subscriptions [$ this ->getTableName ()]['column ' ];
320
344
}
321
345
322
346
/**
@@ -338,7 +362,7 @@ private function getAfterEventTriggerName($event)
338
362
/**
339
363
* Retrieve View related to subscription
340
364
*
341
- * @return \Magento\Framework\Mview\ ViewInterface
365
+ * @return ViewInterface
342
366
* @codeCoverageIgnore
343
367
*/
344
368
public function getView ()
0 commit comments