Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TableMaintainer.php
Go to the documentation of this file.
1 <?php
7 declare(strict_types=1);
9 
15 
20 {
24  private $resource;
25 
29  private $tableResolver;
30 
34  private $connection;
35 
39  private $tmpTableSuffix = '_tmp';
40 
44  private $additionalTableSuffix = '_replica';
45 
49  private $mainTmpTable;
50 
55  public function __construct(
56  ResourceConnection $resource,
57  TableResolver $tableResolver
58  ) {
59  $this->resource = $resource;
60  $this->tableResolver = $tableResolver;
61  }
62 
68  private function getConnection()
69  {
70  if (!isset($this->connection)) {
71  $this->connection = $this->resource->getConnection();
72  }
73  return $this->connection;
74  }
75 
82  private function getTable($table)
83  {
84  return $this->resource->getTableName($table);
85  }
86 
97  private function createTable($mainTableName, $newTableName)
98  {
99  if (!$this->getConnection()->isTableExists($newTableName)) {
100  $this->getConnection()->createTable(
101  $this->getConnection()->createTableByDdl($mainTableName, $newTableName)
102  );
103  }
104  }
105 
113  private function dropTable($tableName)
114  {
115  if ($this->getConnection()->isTableExists($tableName)) {
116  $this->getConnection()->dropTable($tableName);
117  }
118  }
119 
127  public function getMainTable(int $storeId)
128  {
129  $catalogCategoryProductDimension = new Dimension(\Magento\Store\Model\Store::ENTITY, $storeId);
130 
131  return $this->tableResolver->resolve(AbstractAction::MAIN_INDEX_TABLE, [$catalogCategoryProductDimension]);
132  }
133 
143  public function createTablesForStore(int $storeId)
144  {
145  $mainTableName = $this->getMainTable($storeId);
146  //Create index table for store based on main replica table
147  //Using main replica table is necessary for backward capability and TableResolver plugin work
148  $this->createTable(
149  $this->getTable(AbstractAction::MAIN_INDEX_TABLE . $this->additionalTableSuffix),
150  $mainTableName
151  );
152 
153  $mainReplicaTableName = $this->getMainTable($storeId) . $this->additionalTableSuffix;
154  //Create replica table for store based on main replica table
155  $this->createTable(
156  $this->getTable(AbstractAction::MAIN_INDEX_TABLE . $this->additionalTableSuffix),
157  $mainReplicaTableName
158  );
159  }
160 
168  public function dropTablesForStore(int $storeId)
169  {
170  $mainTableName = $this->getMainTable($storeId);
171  $this->dropTable($mainTableName);
172 
173  $mainReplicaTableName = $this->getMainTable($storeId) . $this->additionalTableSuffix;
174  $this->dropTable($mainReplicaTableName);
175  }
176 
184  public function getMainReplicaTable(int $storeId)
185  {
186  return $this->getMainTable($storeId) . $this->additionalTableSuffix;
187  }
188 
196  public function createMainTmpTable(int $storeId)
197  {
198  if (!isset($this->mainTmpTable[$storeId])) {
199  $originTableName = $this->getMainTable($storeId);
200  $temporaryTableName = $this->getMainTable($storeId) . $this->tmpTableSuffix;
201  $this->getConnection()->createTemporaryTableLike($temporaryTableName, $originTableName, true);
202  $this->mainTmpTable[$storeId] = $temporaryTableName;
203  }
204  }
205 
215  public function getMainTmpTable(int $storeId)
216  {
217  if (!isset($this->mainTmpTable[$storeId])) {
218  throw new \Magento\Framework\Exception\NoSuchEntityException('Temporary table does not exist');
219  }
220  return $this->mainTmpTable[$storeId];
221  }
222 }
__construct(ResourceConnection $resource, TableResolver $tableResolver)
$tableName
Definition: trigger.php:13
$resource
Definition: bulk.php:12
$table
Definition: trigger.php:14