Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GridStructure.php
Go to the documentation of this file.
1 <?php
7 
13 
15 {
19  private $resource;
20 
24  private $flatScopeResolver;
25 
29  protected $columnTypesMap = [
30  'varchar' => ['type' => Table::TYPE_TEXT, 'size' => 255],
31  'mediumtext' => ['type' => Table::TYPE_TEXT, 'size' => 16777216],
32  'text' => ['type' => Table::TYPE_TEXT, 'size' => 65536],
33  'int' => ['type' => Table::TYPE_INTEGER, 'size' => null],
34  'date' => ['type' => Table::TYPE_DATE, 'size' => null],
35  'datetime' => ['type' => Table::TYPE_DATETIME, 'size' => null],
36  'timestamp' => ['type' => Table::TYPE_TIMESTAMP, 'size' => null],
37  ];
38 
44  public function __construct(
45  ResourceConnection $resource,
46  FlatScopeResolver $flatScopeResolver,
47  array $columnTypesMap = []
48  ) {
49  $this->resource = $resource;
50  $this->flatScopeResolver = $flatScopeResolver;
51  $this->columnTypesMap = array_merge($this->columnTypesMap, $columnTypesMap);
52  }
53 
59  public function delete($index, array $dimensions = [])
60  {
61  $adapter = $this->getAdapter();
62  $tableName = $this->flatScopeResolver->resolve($index, $dimensions);
63  if ($adapter->isTableExists($tableName)) {
64  $adapter->dropTable($tableName);
65  }
66  }
67 
74  public function create($index, array $fields, array $dimensions = [])
75  {
76  $this->createFlatTable($this->flatScopeResolver->resolve($index, $dimensions), $fields);
77  }
78 
85  protected function createFlatTable($tableName, array $fields)
86  {
87  $adapter = $this->getAdapter();
88  $table = $adapter->newTable($tableName);
89  $table->addColumn(
90  'entity_id',
92  10,
93  ['unsigned' => true, 'nullable' => false, 'primary' => true],
94  'Entity ID'
95  );
96  $searchableFields = [];
97  foreach ($fields as $field) {
98  if ($field['type'] === 'searchable') {
99  $searchableFields[] = $field['name'];
100  }
101  $columnMap = isset($field['dataType']) && isset($this->columnTypesMap[$field['dataType']])
102  ? $this->columnTypesMap[$field['dataType']]
103  : ['type' => $field['dataType'], 'size' => isset($field['size']) ? $field['size'] : null];
104  $name = $field['name'];
105  $type = $columnMap['type'];
106  $size = $columnMap['size'];
107  if ($field['type'] === 'filterable') {
108  $table->addIndex(
109  $this->resource->getIdxName($tableName, $name, AdapterInterface::INDEX_TYPE_INDEX),
110  $name,
112  );
113  }
114  $table->addColumn($name, $type, $size);
115  }
116  $table->addIndex(
117  $this->resource->getIdxName(
118  $tableName,
119  $searchableFields,
121  ),
122  $searchableFields,
124  );
125  $adapter->createTable($table);
126  }
127 
131  private function getAdapter()
132  {
133  $adapter = $this->resource->getConnection('write');
134  return $adapter;
135  }
136 }
$tableName
Definition: trigger.php:13
$fields
Definition: details.phtml:14
create($index, array $fields, array $dimensions=[])
$resource
Definition: bulk.php:12
$adapter
Definition: webapi_user.php:16
$type
Definition: item.phtml:13
createFlatTable($tableName, array $fields)
__construct(ResourceConnection $resource, FlatScopeResolver $flatScopeResolver, array $columnTypesMap=[])
$table
Definition: trigger.php:14
$index
Definition: list.phtml:44
if(!isset($_GET['name'])) $name
Definition: log.php:14