UpdateProductRelations.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GroupedProduct\Setup\Patch\Data;
  7. use Magento\Catalog\Model\ResourceModel\Product\Relation;
  8. use Magento\Framework\DB\Adapter\AdapterInterface;
  9. use Magento\GroupedProduct\Model\ResourceModel\Product\Link;
  10. use Magento\Framework\App\ResourceConnection;
  11. use Magento\Framework\Setup\Patch\DataPatchInterface;
  12. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  13. /**
  14. * Class UpdateProductRelations
  15. * @package Magento\GroupedProduct\Setup\Patch
  16. */
  17. class UpdateProductRelations implements DataPatchInterface, PatchVersionInterface
  18. {
  19. /**
  20. * @var \Magento\Framework\Setup\ModuleDataSetupInterface
  21. */
  22. private $moduleDataSetup;
  23. /**
  24. * @var Relation
  25. */
  26. private $relationProcessor;
  27. /**
  28. * PatchInitial constructor.
  29. * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
  30. */
  31. public function __construct(
  32. \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup,
  33. \Magento\Catalog\Model\ResourceModel\Product\Relation $relationProcessor
  34. ) {
  35. $this->moduleDataSetup = $moduleDataSetup;
  36. $this->relationProcessor = $relationProcessor;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function apply()
  42. {
  43. $this->moduleDataSetup->getConnection()->startSetup();
  44. $connection = $this->moduleDataSetup->getConnection();
  45. $select = $connection->select()
  46. ->from(
  47. $this->relationProcessor->getTable('catalog_product_link'),
  48. ['product_id', 'linked_product_id']
  49. )
  50. ->where('link_type_id = ?', Link::LINK_TYPE_GROUPED);
  51. $connection->query(
  52. $connection->insertFromSelect(
  53. $select,
  54. $this->relationProcessor->getMainTable(),
  55. ['parent_id', 'child_id'],
  56. AdapterInterface::INSERT_IGNORE
  57. )
  58. );
  59. $this->moduleDataSetup->getConnection()->endSetup();
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public static function getDependencies()
  65. {
  66. return [
  67. InitializeGroupedProductLinks::class
  68. ];
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public static function getVersion()
  74. {
  75. return '2.0.1';
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getAliases()
  81. {
  82. return [];
  83. }
  84. }