Skip to content

Commit c955c32

Browse files
author
Oleksii Korshenko
authored
Merge pull request #1356 from magento-engcom/develop-prs
Public Pull Requests #10306 #4893
2 parents 8d0a729 + 1b2fbfc commit c955c32

File tree

4 files changed

+91
-21
lines changed

4 files changed

+91
-21
lines changed

app/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,4 +1330,11 @@
13301330
<argument name="configSource" xsi:type="object">Magento\Config\App\Config\Source\EnvironmentConfigSource</argument>
13311331
</arguments>
13321332
</type>
1333+
<type name="Magento\Framework\Mview\View\Subscription">
1334+
<arguments>
1335+
<argument name="ignoredUpdateColumns" xsi:type="array">
1336+
<item name="updated_at" xsi:type="string">updated_at</item>
1337+
</argument>
1338+
</arguments>
1339+
</type>
13331340
</config>

lib/internal/Magento/Framework/DataObject.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,21 @@ public function convertToXml(
325325
* Convert object data to JSON
326326
*
327327
* @param array $keys array of required keys
328-
* @return string
328+
* @return bool|string
329+
* @throws \InvalidArgumentException
329330
*/
330331
public function toJson(array $keys = [])
331332
{
332333
$data = $this->toArray($keys);
333-
return \Zend_Json::encode($data);
334+
return \Magento\Framework\Serialize\JsonConverter::convert($data);
334335
}
335336

336337
/**
337338
* The "__" style wrapper for toJson
338339
*
339-
* @param array $keys
340-
* @return string
340+
* @param array $keys
341+
* @return bool|string
342+
* @throws \InvalidArgumentException
341343
*/
342344
public function convertToJson(array $keys = [])
343345
{

lib/internal/Magento/Framework/Mview/View/Subscription.php

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\Framework\App\ResourceConnection;
1212
use Magento\Framework\DB\Ddl\Trigger;
13+
use Magento\Framework\Mview\View\StateInterface;
1314

1415
class Subscription implements SubscriptionInterface
1516
{
@@ -52,6 +53,14 @@ class Subscription implements SubscriptionInterface
5253
*/
5354
protected $linkedViews = [];
5455

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+
5564
/**
5665
* @var Resource
5766
*/
@@ -64,14 +73,16 @@ class Subscription implements SubscriptionInterface
6473
* @param \Magento\Framework\Mview\ViewInterface $view
6574
* @param string $tableName
6675
* @param string $columnName
76+
* @param array $ignoredUpdateColumns
6777
*/
6878
public function __construct(
6979
ResourceConnection $resource,
7080
\Magento\Framework\DB\Ddl\TriggerFactory $triggerFactory,
7181
\Magento\Framework\Mview\View\CollectionInterface $viewCollection,
7282
\Magento\Framework\Mview\ViewInterface $view,
7383
$tableName,
74-
$columnName
84+
$columnName,
85+
$ignoredUpdateColumns = []
7586
) {
7687
$this->connection = $resource->getConnection();
7788
$this->triggerFactory = $triggerFactory;
@@ -80,6 +91,7 @@ public function __construct(
8091
$this->tableName = $tableName;
8192
$this->columnName = $columnName;
8293
$this->resource = $resource;
94+
$this->ignoredUpdateColumns = $ignoredUpdateColumns;
8395
}
8496

8597
/**
@@ -154,7 +166,7 @@ public function remove()
154166
protected function getLinkedViews()
155167
{
156168
if (!$this->linkedViews) {
157-
$viewList = $this->viewCollection->getViewsByStateMode(\Magento\Framework\Mview\View\StateInterface::MODE_ENABLED);
169+
$viewList = $this->viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED);
158170

159171
foreach ($viewList as $view) {
160172
/** @var \Magento\Framework\Mview\ViewInterface $view */
@@ -175,35 +187,56 @@ protected function getLinkedViews()
175187
}
176188

177189
/**
178-
* Build trigger statement for INSER, UPDATE, DELETE events
190+
* Build trigger statement for INSERT, UPDATE, DELETE events
179191
*
180192
* @param string $event
181193
* @param \Magento\Framework\Mview\View\ChangelogInterface $changelog
182194
* @return string
183195
*/
184196
protected function buildStatement($event, $changelog)
185197
{
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+
186213
switch ($event) {
187214
case Trigger::EVENT_INSERT:
215+
$trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);";
216+
break;
188217
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;
196227
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;
204230
default:
205231
return '';
232+
206233
}
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+
);
207240
}
208241

209242
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Serialize;
7+
8+
/**
9+
* This class was introducted only for usage in the \Magento\Framework\DataObject::toJson method.
10+
* It should not be used in other cases and instead \Magento\Framework\Serialize\Serializer\Json::serialize
11+
* should be used.
12+
*/
13+
class JsonConverter
14+
{
15+
/**
16+
* This method should only be used by \Magento\Framework\DataObject::toJson
17+
* All other cases should use \Magento\Framework\Serialize\Serializer\Json::serialize directly
18+
*
19+
* @param string|int|float|bool|array|null $data
20+
* @return bool|string
21+
* @throws \InvalidArgumentException
22+
*/
23+
public static function convert($data)
24+
{
25+
$serializer = new \Magento\Framework\Serialize\Serializer\Json();
26+
return $serializer->serialize($data);
27+
}
28+
}

0 commit comments

Comments
 (0)