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
8 
15 
23 {
27  private $resource;
28 
32  private $indexScopeResolver;
33 
37  private $flatScopeResolver;
38 
42  protected $columnTypesMap = [
43  'varchar' => ['type' => Table::TYPE_TEXT, 'size' => 255],
44  'mediumtext' => ['type' => Table::TYPE_TEXT, 'size' => 16777216],
45  'text' => ['type' => Table::TYPE_TEXT, 'size' => 65536],
46  ];
47 
54  public function __construct(
55  ResourceConnection $resource,
56  IndexScopeResolver $indexScopeResolver,
57  FlatScopeResolver $flatScopeResolver,
58  array $columnTypesMap = []
59  ) {
60  $this->resource = $resource;
61  $this->indexScopeResolver = $indexScopeResolver;
62  $this->flatScopeResolver = $flatScopeResolver;
63  $this->columnTypesMap = array_merge($this->columnTypesMap, $columnTypesMap);
64  }
65 
69  public function delete($index, array $dimensions = [])
70  {
71  $this->dropTable($this->resource->getConnection(), $this->indexScopeResolver->resolve($index, $dimensions));
72  $this->dropTable($this->resource->getConnection(), $this->flatScopeResolver->resolve($index, $dimensions));
73  }
74 
78  public function create($index, array $fields, array $dimensions = [])
79  {
80  $this->createFulltextIndex($this->indexScopeResolver->resolve($index, $dimensions));
81  if ($fields) {
82  $this->createFlatIndex($this->flatScopeResolver->resolve($index, $dimensions), $fields);
83  }
84  }
85 
93  protected function createFulltextIndex($tableName)
94  {
95  $table = $this->configureFulltextTable($this->resource->getConnection()->newTable($tableName));
96  $this->resource->getConnection()->createTable($table);
97  }
98 
105  protected function configureFulltextTable(Table $table)
106  {
107  $table->addColumn(
108  'entity_id',
110  10,
111  ['unsigned' => true, 'nullable' => false],
112  'Entity ID'
113  )->addColumn(
114  'attribute_id',
116  255,
117  ['unsigned' => true, 'nullable' => true]
118  )->addColumn(
119  'data_index',
121  '4g',
122  ['nullable' => true],
123  'Data index'
124  )->addIndex(
125  'idx_primary',
126  ['entity_id', 'attribute_id'],
128  )->addIndex(
129  'FTI_FULLTEXT_DATA_INDEX',
130  ['data_index'],
132  );
133  return $table;
134  }
135 
144  protected function createFlatIndex($tableName, array $fields)
145  {
146  $table = $this->resource->getConnection()->newTable($tableName);
147  $table->addColumn(
148  'entity_id',
150  10,
151  ['unsigned' => true, 'nullable' => false],
152  'Entity ID'
153  );
154  foreach ($fields as $field) {
155  if ($field['type'] !== 'filterable') {
156  continue;
157  }
158  $columnMap = isset($field['dataType']) && isset($this->columnTypesMap[$field['dataType']])
159  ? $this->columnTypesMap[$field['dataType']]
160  : ['type' => $field['type'], 'size' => isset($field['size']) ? $field['size'] : null];
161  $name = $field['name'];
162  $type = $columnMap['type'];
163  $size = $columnMap['size'];
164  $table->addColumn($name, $type, $size);
165  }
166  $this->resource->getConnection()->createTable($table);
167  }
168 
176  private function dropTable(AdapterInterface $connection, $tableName)
177  {
178  if ($connection->isTableExists($tableName)) {
179  $connection->dropTable($tableName);
180  }
181  }
182 }
$tableName
Definition: trigger.php:13
__construct(ResourceConnection $resource, IndexScopeResolver $indexScopeResolver, FlatScopeResolver $flatScopeResolver, array $columnTypesMap=[])
$fields
Definition: details.phtml:14
createFlatIndex($tableName, array $fields)
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
create($index, array $fields, array $dimensions=[])
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14
$index
Definition: list.phtml:44
if(!isset($_GET['name'])) $name
Definition: log.php:14