Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateBundleRelatedSchema.php
Go to the documentation of this file.
1 <?php
8 
12 
19 {
23  private $schemaSetup;
24 
29  public function __construct(
30  SchemaSetupInterface $schemaSetup
31  ) {
32  $this->schemaSetup = $schemaSetup;
33  }
34 
38  public function apply()
39  {
40  $this->schemaSetup->startSetup();
41  // Updating data of the 'catalog_product_bundle_option_value' table.
42  $tableName = $this->schemaSetup->getTable('catalog_product_bundle_option_value');
43 
44  $select = $this->schemaSetup->getConnection()->select()
45  ->from(
46  ['values' => $tableName],
47  ['value_id']
48  )->joinLeft(
49  [
50  'options' => $this->schemaSetup->getTable(
51  'catalog_product_bundle_option'
52  )
53  ],
54  'values.option_id = options.option_id',
55  ['parent_product_id' => 'parent_id']
56  );
57 
58  $this->schemaSetup->getConnection()->query(
59  $this->schemaSetup->getConnection()->insertFromSelect(
60  $select,
61  $tableName,
62  ['value_id', 'parent_product_id'],
63  \Magento\Framework\DB\Adapter\AdapterInterface::INSERT_ON_DUPLICATE
64  )
65  );
66 
67  // Updating data of the 'catalog_product_bundle_selection_price' table.
68  $tableName = $this->schemaSetup->getTable(
69  'catalog_product_bundle_selection_price'
70  );
71  $tmpTableName = $this->schemaSetup->getTable(
72  'catalog_product_bundle_selection_price_tmp'
73  );
74 
75  $existingForeignKeys = $this->schemaSetup->getConnection()->getForeignKeys($tableName);
76 
77  foreach ($existingForeignKeys as $key) {
78  $this->schemaSetup->getConnection()->dropForeignKey($key['TABLE_NAME'], $key['FK_NAME']);
79  }
80 
81  $this->schemaSetup->getConnection()->createTable(
82  $this->schemaSetup->getConnection()->createTableByDdl($tableName, $tmpTableName)
83  );
84 
85  foreach ($existingForeignKeys as $key) {
86  $this->schemaSetup->getConnection()->addForeignKey(
87  $key['FK_NAME'],
88  $key['TABLE_NAME'],
89  $key['COLUMN_NAME'],
90  $key['REF_TABLE_NAME'],
91  $key['REF_COLUMN_NAME'],
92  $key['ON_DELETE']
93  );
94  }
95 
96  $this->schemaSetup->getConnection()->query(
97  $this->schemaSetup->getConnection()->insertFromSelect(
98  $this->schemaSetup->getConnection()->select()->from($tableName),
99  $tmpTableName
100  )
101  );
102 
103  $this->schemaSetup->getConnection()->truncateTable($tableName);
104 
105  $columnsToSelect = [];
106 
107  foreach ($this->schemaSetup->getConnection()->describeTable($tmpTableName) as $column) {
108  $alias = $column['COLUMN_NAME'] == 'parent_product_id' ? 'selections.' : 'prices.';
109 
110  $columnsToSelect[] = $alias . $column['COLUMN_NAME'];
111  }
112 
113  $select = $this->schemaSetup->getConnection()->select()
114  ->from(
115  ['prices' => $tmpTableName],
116  []
117  )->joinLeft(
118  [
119  'selections' => $this->schemaSetup->getTable(
120  'catalog_product_bundle_selection'
121  )
122  ],
123  'prices.selection_id = selections.selection_id',
124  []
125  )->columns($columnsToSelect);
126 
127  $this->schemaSetup->getConnection()->query(
128  $this->schemaSetup->getConnection()->insertFromSelect($select, $tableName)
129  );
130 
131  $this->schemaSetup->getConnection()->dropTable($tmpTableName);
132 
133  $this->schemaSetup->endSetup();
134  }
135 
139  public static function getDependencies()
140  {
141  return [];
142  }
143 
147  public static function getVersion()
148  {
149  return '2.0.4';
150  }
151 
155  public function getAliases()
156  {
157  return [];
158  }
159 }
$tableName
Definition: trigger.php:13
if(!trim($html)) $alias
Definition: details.phtml:20