Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexStructure.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
21 {
25  const SKU = 'sku';
26  const QUANTITY = 'quantity';
27  const IS_SALABLE = 'is_salable';
33  private $resourceConnection;
34 
38  private $indexNameResolver;
39 
44  public function __construct(
45  ResourceConnection $resourceConnection,
46  IndexNameResolverInterface $indexNameResolver
47  ) {
48  $this->resourceConnection = $resourceConnection;
49  $this->indexNameResolver = $indexNameResolver;
50  }
51 
55  public function isExist(IndexName $indexName, string $connectionName): bool
56  {
57  $connection = $this->resourceConnection->getConnection($connectionName);
58  $tableName = $this->indexNameResolver->resolveName($indexName);
59  return $connection->isTableExists($this->resourceConnection->getTableName($tableName));
60  }
61 
65  public function create(IndexName $indexName, string $connectionName): void
66  {
67  $connection = $this->resourceConnection->getConnection($connectionName);
68  $tableName = $this->indexNameResolver->resolveName($indexName);
69 
70  if ($connection->isTableExists($tableName)) {
71  throw new StateException(__('Table %table already exits', ['table' => $tableName]));
72  }
73 
74  $this->createTable($connection, $tableName);
75  }
76 
83  private function createTable(\Magento\Framework\DB\Adapter\AdapterInterface $connection, string $tableName)
84  {
85  $table = $connection->newTable(
86  $this->resourceConnection->getTableName($tableName)
87  )->setComment(
88  'Inventory Stock item Table'
89  )->addColumn(
90  self::SKU,
92  64,
93  [
94  Table::OPTION_PRIMARY => true,
95  Table::OPTION_NULLABLE => false,
96  ],
97  'Sku'
98  )->addColumn(
99  self::QUANTITY,
101  null,
102  [
103  Table::OPTION_UNSIGNED => false,
104  Table::OPTION_NULLABLE => false,
107  Table::OPTION_SCALE => 4,
108  ],
109  'Quantity'
110  )->addColumn(
111  self::IS_SALABLE,
113  null,
114  [
115  Table::OPTION_NULLABLE => false,
116  ],
117  'Is Salable'
118  );
119  $connection->createTable($table);
120  }
121 
125  public function delete(IndexName $indexName, string $connectionName): void
126  {
127  $connection = $this->resourceConnection->getConnection($connectionName);
128  $tableName = $this->indexNameResolver->resolveName($indexName);
129  $connection->dropTable($this->resourceConnection->getTableName($tableName));
130  }
131 }
create(IndexName $indexName, string $connectionName)
$tableName
Definition: trigger.php:13
isExist(IndexName $indexName, string $connectionName)
__construct(ResourceConnection $resourceConnection, IndexNameResolverInterface $indexNameResolver)
__()
Definition: __.php:13
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14