Skip to content

Remove product relation foreign key #31048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Plugin\Model\Product\Relation;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\EntityManager\MetadataPool;
use Psr\Log\LoggerInterface;

/**
* Plugin for removing entries from catalog_product_relation after product is deleted
*/
class RemoveRelations
{
/**
* @var MetadataPool
*/
private $metadataPool;

/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param MetadataPool $metadataPool
* @param ResourceConnection $resourceConnection
* @param LoggerInterface $logger
*/
public function __construct(
MetadataPool $metadataPool,
ResourceConnection $resourceConnection,
LoggerInterface $logger
) {
$this->metadataPool = $metadataPool;
$this->resourceConnection = $resourceConnection;
$this->logger = $logger;
}

/**
* Delete related product relations
*
* @param ProductResource $subject
* @param ProductResource $result
* @param ProductInterface $product
* @return ProductResource
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws \Exception
*/
public function afterDelete(
ProductResource $subject,
ProductResource $result,
ProductInterface $product
): ProductResource {
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
$connection = $this->resourceConnection->getConnection();
try {
$connection->beginTransaction();
$relationTable = $this->resourceConnection->getTableName('catalog_product_relation');
$whereCondition = $connection->quoteInto('child_id = ?', $product->getId())
. " OR "
. $connection->quoteInto('parent_id = ?', $product->getData($linkField));
$connection->delete($relationTable, $whereCondition);
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
$this->logger->error(\sprintf(
'Could not delete product relations for product %1$s. %2$s',
$product->getId(),
$e
));
}
return $result;
}
}
6 changes: 0 additions & 6 deletions app/code/Magento/Catalog/etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1069,12 +1069,6 @@
<column name="parent_id"/>
<column name="child_id"/>
</constraint>
<constraint xsi:type="foreign" referenceId="CAT_PRD_RELATION_CHILD_ID_CAT_PRD_ENTT_ENTT_ID"
table="catalog_product_relation" column="child_id" referenceTable="catalog_product_entity"
referenceColumn="entity_id" onDelete="CASCADE"/>
<constraint xsi:type="foreign" referenceId="CAT_PRD_RELATION_PARENT_ID_CAT_PRD_ENTT_ENTT_ID"
table="catalog_product_relation" column="parent_id" referenceTable="catalog_product_entity"
referenceColumn="entity_id" onDelete="CASCADE"/>
<index referenceId="CATALOG_PRODUCT_RELATION_CHILD_ID" indexType="btree">
<column name="child_id"/>
</index>
Expand Down
4 changes: 1 addition & 3 deletions app/code/Magento/Catalog/etc/db_schema_whitelist.json
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,7 @@
"CATALOG_PRODUCT_RELATION_CHILD_ID": true
},
"constraint": {
"PRIMARY": true,
"CAT_PRD_RELATION_CHILD_ID_CAT_PRD_ENTT_ENTT_ID": true,
"CAT_PRD_RELATION_PARENT_ID_CAT_PRD_ENTT_ENTT_ID": true
"PRIMARY": true
}
},
"catalog_product_index_eav": {
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@
<argument name="storage" xsi:type="object">Magento\Catalog\Model\Session\Storage</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\ResourceModel\Product">
<plugin name="removeProductRelationAfterDelete" type="\Magento\Catalog\Plugin\Model\Product\Relation\RemoveRelations"/>
</type>
<type name="Magento\Store\Model\ResourceModel\Website">
<plugin name="invalidatePriceIndexerOnWebsite" type="Magento\Catalog\Model\Indexer\Product\Price\Plugin\Website"/>
<plugin name="categoryProductWebsiteAfterDelete" type="\Magento\Catalog\Model\Indexer\Category\Product\Plugin\Website"/>
Expand Down