Skip to content

Commit 4a2d615

Browse files
Merge pull request #50 from justbetter/feature/remove-redundant-eav
Remove Redundant EAV attribute values
2 parents 3ffd3ed + c82ac70 commit 4a2d615

File tree

4 files changed

+74
-29
lines changed

4 files changed

+74
-29
lines changed

Observer/RemoveRedundantEav.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace JustBetter\AkeneoBundle\Observer;
4+
5+
use Akeneo\Connector\Executor\JobExecutor;
6+
use Akeneo\Connector\Helper\Import\Entities;
7+
use Akeneo\Connector\Helper\Output;
8+
use Magento\Framework\App\ResourceConnection;
9+
use Magento\Framework\Event\Observer;
10+
use Magento\Framework\Event\ObserverInterface;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
13+
class RemoveRedundantEav implements ObserverInterface
14+
{
15+
public const CATALOG_PRODUCT_ENTITY_DATA_TYPES = ['int', 'text', 'decimal', 'gallery', 'varchar', 'datetime'];
16+
17+
public function __construct(
18+
protected Entities $entities,
19+
protected ResourceConnection $resourceConnection,
20+
protected Output $outputHelper,
21+
protected JobExecutor $jobExecutor,
22+
protected ScopeConfigInterface $scopeConfig
23+
) {
24+
}
25+
26+
public function execute(Observer $observer): void
27+
{
28+
if (!$this->scopeConfig->isSetFlag('akeneo_connector/justbetter/remove_redundant_eav')) {
29+
return;
30+
}
31+
32+
$this->jobExecutor->displayInfo((string)$this->outputHelper->getPrefix() . __('Remove Redundant EAV attribute values'));
33+
$connection = $this->resourceConnection->getConnection();
34+
35+
foreach (self::CATALOG_PRODUCT_ENTITY_DATA_TYPES as $dataType) {
36+
$query = "
37+
DELETE cpe{$dataType}
38+
FROM catalog_product_entity_{$dataType} as cpe{$dataType}
39+
LEFT JOIN catalog_product_entity as cpe on cpe.entity_id = cpe{$dataType}.entity_id
40+
LEFT JOIN eav_attribute as ea on ea.attribute_id = cpe{$dataType}.attribute_id
41+
WHERE cpe{$dataType}.store_id = 0 AND cpe.attribute_set_id NOT IN (
42+
SELECT attribute_set_id
43+
FROM eav_entity_attribute
44+
WHERE attribute_set_id = cpe.attribute_set_id AND attribute_id = cpe{$dataType}.attribute_id
45+
)
46+
";
47+
$connection->query($query);
48+
}
49+
}
50+
}

Plugin/InsertNewProducts.php

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,25 @@
44

55
use Akeneo\Connector\Job\Product;
66
use Akeneo\Connector\Helper\Import\Entities;
7-
use Magento\Store\Model\ScopeInterface as scope;
7+
use Magento\Store\Model\ScopeInterface as Scope;
88
use Magento\Framework\App\Config\ScopeConfigInterface;
99

1010
class InsertNewProducts
1111
{
12-
protected $config;
13-
protected $entitiesHelper;
14-
15-
/**
16-
* __construct function
17-
* @param ScopeConfigInterface $config
18-
* @param Entities $entitiesHelper
19-
*/
2012
public function __construct(
21-
ScopeConfigInterface $config,
22-
Entities $entitiesHelper
13+
protected ScopeConfigInterface $config,
14+
protected Entities $entitiesHelper
2315
) {
24-
$this->config = $config;
25-
$this->entitiesHelper = $entitiesHelper;
2616
}
2717

28-
/**
29-
* afterInsertData function
30-
* @param product $subject
31-
* @param bool $result
32-
* @return bool $result
33-
*/
34-
public function afterInsertData(product $subject, $result)
18+
public function afterInsertData(Product $subject)
3519
{
36-
$extensionEnabled = $this->config->getValue('akeneo_connector/justbetter/insertnewproducts', scope::SCOPE_WEBSITE);
37-
if (!$extensionEnabled) {
38-
return $result;
20+
$extensionEnabled = $this->config->getValue('akeneo_connector/justbetter/insertnewproducts', Scope::SCOPE_WEBSITE);
21+
if ($extensionEnabled) {
22+
$connection = $this->entitiesHelper->getConnection();
23+
$tmpTableName = $this->entitiesHelper->getTableName($subject->getCode());
24+
25+
$connection->delete($tmpTableName, ['_is_new = ?' => 1]);
3926
}
40-
41-
$connection = $this->entitiesHelper->getConnection();
42-
$tmpTableName = $this->entitiesHelper->getTableName($subject->getCode());
43-
44-
$connection->delete($tmpTableName, ['_is_new = ?' => 1]);
45-
return $result;
4627
}
4728
}

etc/adminhtml/system.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@
138138
<field id="akeneo_connector/justbetter/defaultstorevalues">1</field>
139139
</depends>
140140
</field>
141+
<field id="remove_redundant_eav" translate="label" type="select" sortOrder="160" showInDefault="1" showInWebsite="0" showInStore="0">
142+
<label>Remove Redundant EAV</label>
143+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
144+
<comment>
145+
<![CDATA[
146+
Enable or disable the removal of redundant EAV attribute values. (Default: no)
147+
<br/><br/>
148+
When a product's family (attribute set) changes in Akeneo, attributes no longer in the new set may still have values stored in the database.
149+
<br/><br/>
150+
Enabling this option removes these redundant values during import, keeping the database clean and consistent.
151+
]]>
152+
</comment>
153+
</field>
141154
<group id="slack" translate="label" type="text" sortOrder="160" showInDefault="1" showInWebsite="0"
142155
showInStore="0">
143156
<label>Slack Akeneo import notifications</label>

etc/events.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
</event>
55
<event name="akeneo_connector_import_finish_product">
66
<observer name="set_not_visible" instance="JustBetter\AkeneoBundle\Observer\SetNotVisible"/>
7+
<observer name="remove_redundant_eav" instance="JustBetter\AkeneoBundle\Observer\RemoveRedundantEav" />
78
</event>
89
</config>

0 commit comments

Comments
 (0)