Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractResource.php
Go to the documentation of this file.
1 <?php
7 
10 
18 {
24  protected $tableStrategy;
25 
33  public function __construct(
34  \Magento\Framework\Model\ResourceModel\Db\Context $context,
35  \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy,
36  $connectionName = null
37  ) {
38  $this->tableStrategy = $tableStrategy;
39  parent::__construct($context, $connectionName);
40  }
41 
47  public function reindexAll()
48  {
49  $this->tableStrategy->setUseIdxTable(true);
50  return $this;
51  }
52 
58  protected function _getIndexAdapter()
59  {
60  return $this->getConnection();
61  }
62 
69  public function getIdxTable($table = null)
70  {
71  if ($table) {
72  return $this->tableStrategy->prepareTableName($table);
73  }
74  return $this->tableStrategy->prepareTableName($this->getMainTable());
75  }
76 
82  public function syncData()
83  {
84  $this->beginTransaction();
85  try {
89  $this->getConnection()->delete($this->getMainTable());
90  $this->insertFromTable($this->getIdxTable(), $this->getMainTable(), false);
91  $this->commit();
92  } catch (\Exception $e) {
93  $this->rollBack();
94  throw $e;
95  }
96  return $this;
97  }
98 
107  public function insertFromTable($sourceTable, $destTable, $readToIndex = true)
108  {
109  if ($readToIndex) {
110  $sourceColumns = array_keys($this->getConnection()->describeTable($sourceTable));
111  $targetColumns = array_keys($this->getConnection()->describeTable($destTable));
112  } else {
113  $sourceColumns = array_keys($this->_getIndexAdapter()->describeTable($sourceTable));
114  $targetColumns = array_keys($this->getConnection()->describeTable($destTable));
115  }
116  $select = $this->_getIndexAdapter()->select()->from($sourceTable, $sourceColumns);
117 
118  $this->insertFromSelect($select, $destTable, $targetColumns, $readToIndex);
119  return $this;
120  }
121 
132  public function insertFromSelect($select, $destTable, array $columns, $readToIndex = true)
133  {
134  if ($readToIndex) {
135  $from = $this->getConnection();
136  $to = $this->_getIndexAdapter();
137  } else {
138  $from = $this->_getIndexAdapter();
139  $to = $this->getConnection();
140  }
141 
142  if ($from === $to) {
143  $query = $select->insertFromSelect($destTable, $columns);
144  $to->query($query);
145  } else {
146  $stmt = $from->query($select);
147  $data = [];
148  $counter = 0;
149  while ($row = $stmt->fetch(\PDO::FETCH_NUM)) {
150  $data[] = $row;
151  $counter++;
152  if ($counter > 2000) {
153  $to->insertArray($destTable, $columns, $data);
154  $data = [];
155  $counter = 0;
156  }
157  }
158  if (!empty($data)) {
159  $to->insertArray($destTable, $columns, $data);
160  }
161  }
162 
163  return $this;
164  }
165 
171  public function clearTemporaryIndexTable()
172  {
173  $this->getConnection()->delete($this->getIdxTable());
174  }
175 }
$columns
Definition: default.phtml:15
insertFromTable($sourceTable, $destTable, $readToIndex=true)
insertFromSelect($select, $destTable, array $columns, $readToIndex=true)
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\Indexer\Table\StrategyInterface $tableStrategy, $connectionName=null)
$table
Definition: trigger.php:14