Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TemporaryStorage.php
Go to the documentation of this file.
1 <?php
8 
14 
24 {
25  const TEMPORARY_TABLE_PREFIX = 'search_tmp_';
26 
27  const FIELD_ENTITY_ID = 'entity_id';
28  const FIELD_SCORE = 'score';
29 
33  private $resource;
34 
38  private $config;
39 
44  public function __construct(
45  \Magento\Framework\App\ResourceConnection $resource,
46  DeploymentConfig $config = null
47  ) {
48  $this->resource = $resource;
49  $this->config = $config !== null ? $config : ObjectManager::getInstance()->get(DeploymentConfig::class);
50  }
51 
59  public function storeDocuments($documents)
60  {
61  return $this->storeApiDocuments($documents);
62  }
63 
71  public function storeApiDocuments($documents)
72  {
73  $data = [];
74  foreach ($documents as $document) {
75  $data[] = [
76  $document->getId(),
77  $document->getCustomAttribute('score')->getValue(),
78  ];
79  }
80 
81  return $this->populateTemporaryTable($this->createTemporaryTable(), $data);
82  }
83 
92  private function populateTemporaryTable(Table $table, $data)
93  {
94  if (count($data)) {
95  $this->getConnection()->insertArray(
96  $table->getName(),
97  [
100  ],
101  $data
102  );
103  }
104  return $table;
105  }
106 
115  {
116  $table = $this->createTemporaryTable();
117  $this->getConnection()->query($this->getConnection()->insertFromSelect($select, $table->getName()));
118  return $table;
119  }
120 
126  private function getConnection()
127  {
128  return $this->resource->getConnection();
129  }
130 
137  private function createTemporaryTable()
138  {
139  $connection = $this->getConnection();
140  $tableName = $this->resource->getTableName(str_replace('.', '_', uniqid(self::TEMPORARY_TABLE_PREFIX, true)));
141  $table = $connection->newTable($tableName);
142  if ($this->config->get('db/connection/indexer/persistent')) {
143  $connection->dropTemporaryTable($table->getName());
144  }
145  $table->addColumn(
146  self::FIELD_ENTITY_ID,
148  10,
149  ['unsigned' => true, 'nullable' => false, 'primary' => true],
150  'Entity ID'
151  );
152  $table->addColumn(
153  self::FIELD_SCORE,
155  [32, 16],
156  ['unsigned' => true, 'nullable' => false],
157  'Score'
158  );
159  $table->setOption('type', 'memory');
160  $connection->createTemporaryTable($table);
161  return $table;
162  }
163 }
$tableName
Definition: trigger.php:13
$resource
Definition: bulk.php:12
__construct(\Magento\Framework\App\ResourceConnection $resource, DeploymentConfig $config=null)
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14