forked from aligent/magento2-prerender-io
-
Notifications
You must be signed in to change notification settings - Fork 0
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
BEG-151: Add custom StagedProductAttributeSubscription for catalog_product_link table #13 #14
Merged
aligent-lturner
merged 4 commits into
main
from
feature/BEG-151_fix_for_issue_related_to_row_id_and_entity_id_both_being_added_to__cl_tables
Mar 18, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fd9084f
BEG-151: Add custom StagedProductAttributeSubscription for catalog_pr…
mausham-shrestha-aligent 6bbdb8c
BEG-151: Added module dependencies #13
mausham-shrestha-aligent f4e79ba
BEG-151: Update subscription class to be specific to "catalog_product…
mausham-shrestha-aligent 9df04ae
BEG-151: Use type instead of virtualType and include subscription_mod…
mausham-shrestha-aligent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
221 changes: 221 additions & 0 deletions
221
Model/Mview/View/Attribute/CatalogProductLinkSubscription.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
<?php | ||
/* | ||
* Copyright (c) Aligent Consulting. All rights reserved. | ||
*/ | ||
namespace Aligent\Prerender\Model\Mview\View\Attribute; | ||
|
||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
use Magento\Framework\DB\Ddl\Trigger; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Ddl\TriggerFactory; | ||
use Magento\Framework\EntityManager\EntityMetadata; | ||
use Magento\Framework\EntityManager\MetadataPool; | ||
use Magento\Framework\Mview\View\ChangelogInterface; | ||
use Magento\Framework\Mview\View\CollectionInterface; | ||
use Magento\Framework\Mview\ViewInterface; | ||
use Magento\Framework\Mview\Config; | ||
use Magento\Framework\Mview\View\Changelog; | ||
|
||
/** | ||
* Subscription model class for "catalog_product_link" table | ||
*/ | ||
class CatalogProductLinkSubscription extends \Magento\Framework\Mview\View\Subscription | ||
{ | ||
private const CATALOG_PRODUCT_LINK_PRODUCT_ID = 'product_id'; | ||
|
||
/** | ||
* @var EntityMetadata | ||
*/ | ||
protected $entityMetadata; | ||
|
||
/** | ||
* Save state of Subscription for build statement for retrieving entity id value | ||
* | ||
* @var array | ||
*/ | ||
private $statementState = []; | ||
|
||
/** | ||
* List of columns that can be updated in a subscribed table | ||
* without creating a new change log entry | ||
* | ||
* @var array | ||
*/ | ||
private $ignoredUpdateColumns; | ||
|
||
/** | ||
* @param ResourceConnection $resource | ||
* @param TriggerFactory $triggerFactory | ||
* @param CollectionInterface $viewCollection | ||
* @param ViewInterface $view | ||
* @param string $tableName | ||
* @param string $columnName | ||
* @param MetadataPool $metadataPool | ||
* @param string|null $entityInterface | ||
* @param array $ignoredUpdateColumns | ||
* @param array $ignoredUpdateColumnsBySubscription | ||
* @param Config|null $mviewConfig | ||
* @throws \Exception | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resource, | ||
TriggerFactory $triggerFactory, | ||
CollectionInterface $viewCollection, | ||
ViewInterface $view, | ||
$tableName, | ||
$columnName, | ||
MetadataPool $metadataPool, | ||
$entityInterface = null, | ||
$ignoredUpdateColumns = [], | ||
$ignoredUpdateColumnsBySubscription = [], | ||
Config $mviewConfig = null | ||
) { | ||
parent::__construct( | ||
$resource, | ||
$triggerFactory, | ||
$viewCollection, | ||
$view, | ||
$tableName, | ||
$columnName, | ||
$ignoredUpdateColumns, | ||
$ignoredUpdateColumnsBySubscription, | ||
$mviewConfig | ||
); | ||
$this->ignoredUpdateColumns = $ignoredUpdateColumns; | ||
$this->entityMetadata = $metadataPool->getMetadata($entityInterface); | ||
} | ||
|
||
/** | ||
* Build trigger statement for INSERT, UPDATE, DELETE events | ||
* | ||
* @param string $event | ||
* @param ViewInterface $view | ||
* @return string | ||
*/ | ||
protected function buildStatement(string $event, ViewInterface $view): string | ||
{ | ||
$triggerBody = ''; | ||
|
||
switch ($event) { | ||
case Trigger::EVENT_INSERT: | ||
case Trigger::EVENT_UPDATE: | ||
$eventType = 'NEW'; | ||
break; | ||
case Trigger::EVENT_DELETE: | ||
$eventType = 'OLD'; | ||
break; | ||
default: | ||
return $triggerBody; | ||
} | ||
$entityIdHash = $this->entityMetadata->getIdentifierField() | ||
. $this->entityMetadata->getEntityTable() | ||
. $this->entityMetadata->getLinkField() | ||
. $event; | ||
if (!isset($this->statementState[$entityIdHash])) { | ||
$triggerBody = $this->buildEntityIdStatementByEventType($eventType); | ||
$this->statementState[$entityIdHash] = true; | ||
} | ||
|
||
$trigger = $this->buildStatementByEventType($view, $event); | ||
if ($event == Trigger::EVENT_UPDATE) { | ||
$trigger = $this->addConditionsToTrigger($trigger); | ||
} | ||
$triggerBody .= $trigger; | ||
|
||
return $triggerBody; | ||
} | ||
|
||
/** | ||
* Adds quoted conditions to the trigger | ||
* | ||
* @param string $trigger | ||
* @return string | ||
*/ | ||
private function addConditionsToTrigger(string $trigger): string | ||
{ | ||
$tableName = $this->resource->getTableName($this->getTableName()); | ||
if ($this->connection->isTableExists($tableName) | ||
&& $describe = $this->connection->describeTable($tableName) | ||
) { | ||
$columnNames = array_column($describe, 'COLUMN_NAME'); | ||
$columnNames = array_diff($columnNames, $this->ignoredUpdateColumns); | ||
if ($columnNames) { | ||
$columns = []; | ||
foreach ($columnNames as $columnName) { | ||
$columns[] = sprintf( | ||
'NOT(NEW.%1$s <=> OLD.%1$s)', | ||
$this->connection->quoteIdentifier($columnName) | ||
); | ||
} | ||
$trigger = sprintf( | ||
"IF (%s) THEN %s END IF;", | ||
implode(' OR ', $columns), | ||
$trigger | ||
); | ||
} | ||
} | ||
|
||
return $trigger; | ||
} | ||
|
||
/** | ||
* Build trigger body | ||
* | ||
* @param string $eventType | ||
* @return string | ||
*/ | ||
private function buildEntityIdStatementByEventType(string $eventType): string | ||
{ | ||
return vsprintf( | ||
'SET @entity_id = (SELECT %1$s FROM %2$s WHERE %3$s = %4$s.%5$s);', | ||
[ | ||
$this->connection->quoteIdentifier( | ||
$this->entityMetadata->getIdentifierField() | ||
), | ||
$this->connection->quoteIdentifier( | ||
$this->resource->getTableName($this->entityMetadata->getEntityTable()) | ||
), | ||
$this->connection->quoteIdentifier( | ||
$this->entityMetadata->getLinkField() | ||
), | ||
$eventType, | ||
self::CATALOG_PRODUCT_LINK_PRODUCT_ID | ||
] | ||
) . PHP_EOL; | ||
} | ||
|
||
/** | ||
* Override column name as we need to use entity_id instead of link field | ||
* | ||
* @param string $prefix | ||
* @param ViewInterface $view | ||
* @return string | ||
*/ | ||
public function getEntityColumn(string $prefix, ViewInterface $view): string | ||
{ | ||
return '@entity_id'; | ||
} | ||
|
||
/** | ||
* Build sql statement for trigger | ||
* | ||
* @param ViewInterface $view | ||
* @param string $event | ||
* @return string | ||
* @throws \Exception | ||
*/ | ||
private function buildStatementByEventType(ViewInterface $view, string $event): string | ||
{ | ||
$columns = $this->prepareColumns($view, $event); | ||
return vsprintf( | ||
'INSERT IGNORE INTO %s (%s) values(%s);', | ||
[ | ||
$this->connection->quoteIdentifier( | ||
$this->resource->getTableName($view->getChangelog()->getName()) | ||
), | ||
implode(', ', $columns['column_names']), | ||
implode(', ', $columns['column_values']) | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mausham-shrestha-aligent Please rename the module namespace PrerenderIo to Prerender otherwise we're getting error.
Warning: class_implements(): Class Aligent\PrerenderIo\Model\Mview\View\Attribute\CatalogProductLinkSubscription does not exist and could not be loaded in /app/vendor/magento/framework/Mview/Config/Converter.php on line 108