10
10
11
11
use Magento \Framework \App \ResourceConnection ;
12
12
use Magento \Framework \DB \Ddl \Trigger ;
13
+ use Magento \Framework \Mview \View \StateInterface ;
13
14
14
15
class Subscription implements SubscriptionInterface
15
16
{
@@ -52,6 +53,14 @@ class Subscription implements SubscriptionInterface
52
53
*/
53
54
protected $ linkedViews = [];
54
55
56
+ /**
57
+ * List of columns that can be updated in a subscribed table
58
+ * without creating a new change log entry
59
+ *
60
+ * @var array
61
+ */
62
+ private $ ignoredUpdateColumns = [];
63
+
55
64
/**
56
65
* @var Resource
57
66
*/
@@ -64,14 +73,16 @@ class Subscription implements SubscriptionInterface
64
73
* @param \Magento\Framework\Mview\ViewInterface $view
65
74
* @param string $tableName
66
75
* @param string $columnName
76
+ * @param array $ignoredUpdateColumns
67
77
*/
68
78
public function __construct (
69
79
ResourceConnection $ resource ,
70
80
\Magento \Framework \DB \Ddl \TriggerFactory $ triggerFactory ,
71
81
\Magento \Framework \Mview \View \CollectionInterface $ viewCollection ,
72
82
\Magento \Framework \Mview \ViewInterface $ view ,
73
83
$ tableName ,
74
- $ columnName
84
+ $ columnName ,
85
+ $ ignoredUpdateColumns = []
75
86
) {
76
87
$ this ->connection = $ resource ->getConnection ();
77
88
$ this ->triggerFactory = $ triggerFactory ;
@@ -80,6 +91,7 @@ public function __construct(
80
91
$ this ->tableName = $ tableName ;
81
92
$ this ->columnName = $ columnName ;
82
93
$ this ->resource = $ resource ;
94
+ $ this ->ignoredUpdateColumns = $ ignoredUpdateColumns ;
83
95
}
84
96
85
97
/**
@@ -154,7 +166,7 @@ public function remove()
154
166
protected function getLinkedViews ()
155
167
{
156
168
if (!$ this ->linkedViews ) {
157
- $ viewList = $ this ->viewCollection ->getViewsByStateMode (\ Magento \ Framework \ Mview \ View \ StateInterface::MODE_ENABLED );
169
+ $ viewList = $ this ->viewCollection ->getViewsByStateMode (StateInterface::MODE_ENABLED );
158
170
159
171
foreach ($ viewList as $ view ) {
160
172
/** @var \Magento\Framework\Mview\ViewInterface $view */
@@ -175,35 +187,56 @@ protected function getLinkedViews()
175
187
}
176
188
177
189
/**
178
- * Build trigger statement for INSER , UPDATE, DELETE events
190
+ * Build trigger statement for INSERT , UPDATE, DELETE events
179
191
*
180
192
* @param string $event
181
193
* @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
182
194
* @return string
183
195
*/
184
196
protected function buildStatement ($ event , $ changelog )
185
197
{
198
+ $ columns = [];
199
+ if ($ this ->connection ->isTableExists ($ this ->getTableName ())
200
+ && $ describe = $ this ->connection ->describeTable ($ this ->getTableName ())
201
+ ) {
202
+ foreach ($ describe as $ column ) {
203
+ if (in_array ($ column ['COLUMN_NAME ' ], $ this ->ignoredUpdateColumns )) {
204
+ continue ;
205
+ }
206
+ $ columns [] = sprintf (
207
+ 'NEW.%1$s != OLD.%1$s ' ,
208
+ $ this ->connection ->quoteIdentifier ($ column ['COLUMN_NAME ' ])
209
+ );
210
+ }
211
+ }
212
+
186
213
switch ($ event ) {
187
214
case Trigger::EVENT_INSERT :
215
+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ;
216
+ break ;
188
217
case Trigger::EVENT_UPDATE :
189
- return sprintf (
190
- "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ,
191
- $ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
192
- $ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ()),
193
- $ this ->connection ->quoteIdentifier ($ this ->getColumnName ())
194
- );
195
-
218
+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s); " ;
219
+ if ($ columns ) {
220
+ $ trigger = sprintf (
221
+ "IF (%s) THEN %s END IF; " ,
222
+ implode (' OR ' , $ columns ),
223
+ $ trigger
224
+ );
225
+ }
226
+ break ;
196
227
case Trigger::EVENT_DELETE :
197
- return sprintf (
198
- "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s); " ,
199
- $ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
200
- $ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ()),
201
- $ this ->connection ->quoteIdentifier ($ this ->getColumnName ())
202
- );
203
-
228
+ $ trigger = "INSERT IGNORE INTO %s (%s) VALUES (OLD.%s); " ;
229
+ break ;
204
230
default :
205
231
return '' ;
232
+
206
233
}
234
+ return sprintf (
235
+ $ trigger ,
236
+ $ this ->connection ->quoteIdentifier ($ this ->resource ->getTableName ($ changelog ->getName ())),
237
+ $ this ->connection ->quoteIdentifier ($ changelog ->getColumnName ()),
238
+ $ this ->connection ->quoteIdentifier ($ this ->getColumnName ())
239
+ );
207
240
}
208
241
209
242
/**
0 commit comments