Recurring.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Reports\Setup;
  7. use Magento\Framework\Setup\ExternalFKSetup;
  8. use Magento\Framework\Setup\InstallSchemaInterface;
  9. use Magento\Framework\Setup\ModuleContextInterface;
  10. use Magento\Framework\Setup\SchemaSetupInterface;
  11. use Magento\Framework\EntityManager\MetadataPool;
  12. use Magento\Catalog\Api\Data\ProductInterface;
  13. /**
  14. * @codeCoverageIgnore
  15. */
  16. class Recurring implements InstallSchemaInterface
  17. {
  18. /**
  19. * @var MetadataPool
  20. */
  21. protected $metadataPool;
  22. /**
  23. * @var ExternalFKSetup
  24. */
  25. protected $externalFKSetup;
  26. /**
  27. * @param MetadataPool $metadataPool
  28. * @param ExternalFKSetup $externalFKSetup
  29. */
  30. public function __construct(
  31. MetadataPool $metadataPool,
  32. ExternalFKSetup $externalFKSetup
  33. ) {
  34. $this->metadataPool = $metadataPool;
  35. $this->externalFKSetup = $externalFKSetup;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
  41. {
  42. $installer = $setup;
  43. $installer->startSetup();
  44. $listTables = [
  45. 'report_viewed_product_aggregated_daily' => 'product_id',
  46. 'report_viewed_product_aggregated_monthly' => 'product_id',
  47. 'report_viewed_product_aggregated_yearly' => 'product_id',
  48. 'report_compared_product_index' => 'product_id',
  49. 'report_viewed_product_index' => 'product_id'
  50. ];
  51. foreach ($listTables as $tableName => $columnName) {
  52. $this->addExternalForeignKeys($installer, $tableName, $columnName);
  53. }
  54. $installer->endSetup();
  55. }
  56. /**
  57. * Add external foreign keys
  58. *
  59. * @param SchemaSetupInterface $installer
  60. * @param string $tableName
  61. * @param string $columnName
  62. * @return void
  63. * @throws \Exception
  64. */
  65. protected function addExternalForeignKeys(SchemaSetupInterface $installer, $tableName, $columnName)
  66. {
  67. $metadata = $this->metadataPool->getMetadata(ProductInterface::class);
  68. $this->externalFKSetup->install(
  69. $installer,
  70. $metadata->getEntityTable(),
  71. $metadata->getIdentifierField(),
  72. $tableName,
  73. $columnName
  74. );
  75. }
  76. }