Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Database.php
Go to the documentation of this file.
1 <?php
7 
15 {
19  protected $_resourceHelper;
20 
26  public function __construct(
27  \Magento\Framework\Model\ResourceModel\Db\Context $context,
28  \Magento\Framework\DB\Helper $resourceHelper,
29  $connectionName = null
30  ) {
31  parent::__construct($context, $connectionName);
32  $this->_resourceHelper = $resourceHelper;
33  }
34 
40  protected function _construct()
41  {
42  $this->_init('media_storage_file_storage', 'file_id');
43  }
44 
50  public function createDatabaseScheme()
51  {
52  $connection = $this->getConnection();
53  $table = $this->getMainTable();
54  if ($connection->isTableExists($table)) {
55  return $this;
56  }
57 
58  $dirStorageTable = $this->getTable('media_storage_directory_storage');
59  // For foreign key
60 
61  $ddlTable = $connection->newTable(
62  $table
63  )->addColumn(
64  'file_id',
65  \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
66  null,
67  ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
68  'File Id'
69  )->addColumn(
70  'content',
71  \Magento\Framework\DB\Ddl\Table::TYPE_VARBINARY,
72  \Magento\Framework\DB\Ddl\Table::MAX_VARBINARY_SIZE,
73  ['nullable' => false],
74  'File Content'
75  )->addColumn(
76  'upload_time',
77  \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
78  null,
79  ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
80  'Upload Timestamp'
81  )->addColumn(
82  'filename',
83  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
84  100,
85  ['nullable' => false],
86  'Filename'
87  )->addColumn(
88  'directory_id',
89  \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
90  null,
91  ['unsigned' => true, 'default' => null],
92  'Identifier of Directory where File is Located'
93  )->addColumn(
94  'directory',
95  \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
96  255,
97  ['default' => null],
98  'Directory Path'
99  )->addIndex(
100  $connection->getIndexName(
101  $table,
102  ['filename', 'directory_id'],
103  \Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_UNIQUE
104  ),
105  ['filename', 'directory_id'],
107  )->addIndex(
108  $connection->getIndexName($table, ['directory_id']),
109  ['directory_id']
110  )->addForeignKey(
111  $connection->getForeignKeyName($table, 'directory_id', $dirStorageTable, 'directory_id'),
112  'directory_id',
113  $dirStorageTable,
114  'directory_id',
116  )->setComment(
117  'File Storage'
118  );
119 
120  $connection->createTable($ddlTable);
121  return $this;
122  }
123 
130  protected function _decodeFileContent($row)
131  {
132  $row['content'] = $this->getConnection()->decodeVarbinary($row['content']);
133  return $row;
134  }
135 
142  protected function _decodeAllFilesContent($rows)
143  {
144  foreach ($rows as $key => $row) {
145  $rows[$key] = $this->_decodeFileContent($row);
146  }
147  return $rows;
148  }
149 
158  public function loadByFilename(\Magento\MediaStorage\Model\File\Storage\Database $object, $filename, $path)
159  {
160  $connection = $this->getConnection();
161 
162  $select = $connection->select()->from(
163  ['e' => $this->getMainTable()]
164  )->where(
165  'filename = ?',
166  $filename
167  )->where(
168  $connection->prepareSqlCondition('directory', ['seq' => $path])
169  );
170 
171  $row = $connection->fetchRow($select);
172  if ($row) {
173  $row = $this->_decodeFileContent($row);
174  $object->setData($row);
175  $this->_afterLoad($object);
176  }
177 
178  return $this;
179  }
180 
186  public function clearFiles()
187  {
188  $connection = $this->getConnection();
189  $connection->delete($this->getMainTable());
190 
191  return $this;
192  }
193 
201  public function getFiles($offset = 0, $count = 100)
202  {
203  $connection = $this->getConnection();
204 
205  $select = $connection->select()->from(
206  ['e' => $this->getMainTable()],
207  ['filename', 'content', 'directory']
208  )->order(
209  'file_id'
210  )->limit(
211  $count,
212  $offset
213  );
214 
215  $rows = $connection->fetchAll($select);
216  return $this->_decodeAllFilesContent($rows);
217  }
218 
225  public function saveFile($file)
226  {
227  $connection = $this->getConnection();
228 
229  $contentParam = new \Magento\Framework\DB\Statement\Parameter($file['content']);
230  $contentParam->setIsBlob(true);
231  $data = [
232  'content' => $contentParam,
233  'upload_time' => $file['update_time'],
234  'filename' => $file['filename'],
235  'directory_id' => $file['directory_id'],
236  'directory' => $file['directory'],
237  ];
238 
239  $connection->insertOnDuplicate($this->getMainTable(), $data, ['content', 'upload_time']);
240 
241  return $this;
242  }
243 
253  public function renameFile($oldFilename, $oldPath, $newFilename, $newPath)
254  {
255  $connection = $this->getConnection();
256  $dataUpdate = ['filename' => $newFilename, 'directory' => $newPath];
257 
258  $dataWhere = ['filename = ?' => $oldFilename];
259  $dataWhere[] = new \Zend_Db_Expr($connection->prepareSqlCondition('directory', ['seq' => $oldPath]));
260 
261  $connection->update($this->getMainTable(), $dataUpdate, $dataWhere);
262 
263  return $this;
264  }
265 
275  public function copyFile($oldFilename, $oldPath, $newFilename, $newPath)
276  {
277  $connection = $this->getConnection();
278 
279  $select = $connection->select()->from(
280  ['e' => $this->getMainTable()]
281  )->where(
282  'filename = ?',
283  $oldFilename
284  )->where(
285  $connection->prepareSqlCondition('directory', ['seq' => $oldPath])
286  );
287 
288  $data = $connection->fetchRow($select);
289  if (!$data) {
290  return $this;
291  }
292 
293  if (isset($data['file_id']) && isset($data['filename'])) {
294  unset($data['file_id']);
295  $data['filename'] = $newFilename;
296  $data['directory'] = $newPath;
297 
298  $connection = $this->getConnection();
299  $connection->insertOnDuplicate($this->getMainTable(), $data, ['content', 'upload_time']);
300  }
301 
302  return $this;
303  }
304 
312  public function fileExists($filename, $path)
313  {
314  $connection = $this->getConnection();
315 
316  $select = $connection->select()->from(
317  ['e' => $this->getMainTable()]
318  )->where(
319  'filename = ?',
320  $filename
321  )->where(
322  $connection->prepareSqlCondition('directory', ['seq' => $path])
323  )->limit(
324  1
325  );
326 
327  $data = $connection->fetchRow($select);
328  return (bool)$data;
329  }
330 
337  public function deleteFolder($folderName = '')
338  {
339  $folderName = rtrim($folderName, '/');
340  if (!strlen($folderName)) {
341  return;
342  }
343 
344  $likeExpression = $this->_resourceHelper->addLikeEscape($folderName . '/', ['position' => 'start']);
345  $this->getConnection()->delete(
346  $this->getMainTable(),
347  new \Zend_Db_Expr('filename LIKE ' . $likeExpression)
348  );
349  }
350 
358  public function deleteFile($filename, $directory)
359  {
360  $connection = $this->getConnection();
361 
362  $where = ['filename = ?' => $filename];
363  $where[] = new \Zend_Db_Expr($connection->prepareSqlCondition('directory', ['seq' => $directory]));
364 
365  $connection->delete($this->getMainTable(), $where);
366  }
367 
374  public function getDirectoryFiles($directory)
375  {
376  $directory = trim($directory, '/');
377  $connection = $this->getConnection();
378 
379  $select = $connection->select()->from(
380  ['e' => $this->getMainTable()],
381  ['filename', 'directory', 'content']
382  )->where(
383  $connection->prepareSqlCondition('directory', ['seq' => $directory])
384  )->order(
385  'file_id'
386  );
387 
388  $rows = $connection->fetchAll($select);
389  return $this->_decodeAllFilesContent($rows);
390  }
391 }
loadByFilename(\Magento\MediaStorage\Model\File\Storage\Database $object, $filename, $path)
Definition: Database.php:158
$count
Definition: recent.phtml:13
renameFile($oldFilename, $oldPath, $newFilename, $newPath)
Definition: Database.php:253
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\DB\Helper $resourceHelper, $connectionName=null)
Definition: Database.php:26
copyFile($oldFilename, $oldPath, $newFilename, $newPath)
Definition: Database.php:275
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:641