Skip to content

Commit 6dfb2cc

Browse files
committed
#27499: Filtering data for insert
1 parent ce08d34 commit 6dfb2cc

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

app/code/Magento/MediaGallery/Model/Asset/Command/Save.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace Magento\MediaGallery\Model\Asset\Command;
99

10-
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
11-
use Magento\MediaGalleryApi\Model\Asset\Command\SaveInterface;
1210
use Magento\Framework\App\ResourceConnection;
1311
use Magento\Framework\Exception\CouldNotSaveException;
1412
use Magento\Framework\Reflection\DataObjectProcessor;
13+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
14+
use Magento\MediaGalleryApi\Model\Asset\Command\SaveInterface;
1515
use Psr\Log\LoggerInterface;
1616

1717
/**
@@ -72,7 +72,7 @@ public function execute(AssetInterface $mediaAsset): int
7272

7373
$connection->insertOnDuplicate(
7474
$tableName,
75-
array_filter($this->objectProcessor->buildOutputDataArray($mediaAsset, AssetInterface::class))
75+
$this->filterData($this->objectProcessor->buildOutputDataArray($mediaAsset, AssetInterface::class))
7676
);
7777
return (int) $connection->lastInsertId($tableName);
7878
} catch (\Exception $exception) {
@@ -81,4 +81,28 @@ public function execute(AssetInterface $mediaAsset): int
8181
throw new CouldNotSaveException($message, $exception);
8282
}
8383
}
84+
85+
/**
86+
* Filter data to get flat array without null values
87+
*
88+
* @param array $data
89+
* @return array
90+
*/
91+
private function filterData(array $data): array
92+
{
93+
$filteredData = [];
94+
foreach ($data as $key => $value) {
95+
if ($value === null) {
96+
continue;
97+
}
98+
if (is_array($value)) {
99+
continue;
100+
}
101+
if (is_object($value)) {
102+
continue;
103+
}
104+
$filteredData[$key] = $value;
105+
}
106+
return $filteredData;
107+
}
84108
}

app/code/Magento/MediaGallery/Model/ResourceModel/SaveAssets.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function execute(array $assets): void
6666
try {
6767
$connection->insertOnDuplicate(
6868
$tableName,
69-
array_filter($this->objectProcessor->buildOutputDataArray($asset, AssetInterface::class))
69+
$this->filterData($this->objectProcessor->buildOutputDataArray($asset, AssetInterface::class))
7070
);
7171
} catch (\Exception $exception) {
7272
$this->logger->critical($exception);
@@ -85,4 +85,28 @@ public function execute(array $assets): void
8585
);
8686
}
8787
}
88+
89+
/**
90+
* Filter data to get flat array without null values
91+
*
92+
* @param array $data
93+
* @return array
94+
*/
95+
private function filterData(array $data): array
96+
{
97+
$filteredData = [];
98+
foreach ($data as $key => $value) {
99+
if ($value === null) {
100+
continue;
101+
}
102+
if (is_array($value)) {
103+
continue;
104+
}
105+
if (is_object($value)) {
106+
continue;
107+
}
108+
$filteredData[$key] = $value;
109+
}
110+
return $filteredData;
111+
}
88112
}

0 commit comments

Comments
 (0)