Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Changelog.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class Changelog implements ChangelogInterface
12 {
16  const NAME_SUFFIX = 'cl';
17 
21  const COLUMN_NAME = 'entity_id';
22 
28  protected $connection;
29 
35  protected $viewId;
36 
40  protected $resource;
41 
45  public function __construct(\Magento\Framework\App\ResourceConnection $resource)
46  {
47  $this->connection = $resource->getConnection();
48  $this->resource = $resource;
49  $this->checkConnection();
50  }
51 
58  protected function checkConnection()
59  {
60  if (!$this->connection) {
61  throw new \Exception("The write connection to the database isn't available. Please try again later.");
62  }
63  }
64 
71  public function create()
72  {
73  $changelogTableName = $this->resource->getTableName($this->getName());
74  if (!$this->connection->isTableExists($changelogTableName)) {
75  $table = $this->connection->newTable(
76  $changelogTableName
77  )->addColumn(
78  'version_id',
79  \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
80  null,
81  ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
82  'Version ID'
83  )->addColumn(
84  $this->getColumnName(),
85  \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
86  null,
87  ['unsigned' => true, 'nullable' => false, 'default' => '0'],
88  'Entity ID'
89  );
90  $this->connection->createTable($table);
91  }
92  }
93 
100  public function drop()
101  {
102  $changelogTableName = $this->resource->getTableName($this->getName());
103  if (!$this->connection->isTableExists($changelogTableName)) {
104  throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
105  }
106 
107  $this->connection->dropTable($changelogTableName);
108  }
109 
117  public function clear($versionId)
118  {
119  $changelogTableName = $this->resource->getTableName($this->getName());
120  if (!$this->connection->isTableExists($changelogTableName)) {
121  throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
122  }
123 
124  $this->connection->delete($changelogTableName, ['version_id < ?' => (int)$versionId]);
125 
126  return true;
127  }
128 
137  public function getList($fromVersionId, $toVersionId)
138  {
139  $changelogTableName = $this->resource->getTableName($this->getName());
140  if (!$this->connection->isTableExists($changelogTableName)) {
141  throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
142  }
143 
144  $select = $this->connection->select()->distinct(
145  true
146  )->from(
147  $changelogTableName,
148  [$this->getColumnName()]
149  )->where(
150  'version_id > ?',
151  (int)$fromVersionId
152  )->where(
153  'version_id <= ?',
154  (int)$toVersionId
155  );
156 
157  return $this->connection->fetchCol($select);
158  }
159 
166  public function getVersion()
167  {
168  $changelogTableName = $this->resource->getTableName($this->getName());
169  if (!$this->connection->isTableExists($changelogTableName)) {
170  throw new ChangelogTableNotExistsException(new Phrase("Table %1 does not exist", [$changelogTableName]));
171  }
172  $row = $this->connection->fetchRow('SHOW TABLE STATUS LIKE ?', [$changelogTableName]);
173  if (isset($row['Auto_increment'])) {
174  return (int)$row['Auto_increment'] - 1;
175  } else {
176  throw new \Exception("Table status for `{$changelogTableName}` is incorrect. Can`t fetch version id.");
177  }
178  }
179 
188  public function getName()
189  {
190  if (strlen($this->viewId) == 0) {
191  throw new \Exception("View's identifier is not set");
192  }
193  return $this->viewId . '_' . self::NAME_SUFFIX;
194  }
195 
201  public function getColumnName()
202  {
203  return self::COLUMN_NAME;
204  }
205 
212  public function setViewId($viewId)
213  {
214  $this->viewId = $viewId;
215  return $this;
216  }
217 
221  public function getViewId()
222  {
223  return $this->viewId;
224  }
225 }
__construct(\Magento\Framework\App\ResourceConnection $resource)
Definition: Changelog.php:45
getList($fromVersionId, $toVersionId)
Definition: Changelog.php:137
$table
Definition: trigger.php:14