Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Gallery Class Reference
Inheritance diagram for Gallery:
AbstractDb AbstractResource

Public Member Functions

 __construct (\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\EntityManager\MetadataPool $metadataPool, $connectionName=null)
 
 getConnection ()
 
 loadDataFromTableByValueId ( $tableNameAlias, array $ids, $storeId=null, array $cols=null, array $leftJoinTables=[], $whereCondition=null)
 
 loadProductGalleryByAttributeId ($product, $attributeId)
 
 createBatchBaseSelect ($storeId, $attributeId)
 
 getMainTableAlias ()
 
 bindValueToEntity ($valueId, $entityId)
 
 saveDataRow ($table, array $data, array $fields=[])
 
 insertGallery ($data)
 
 deleteGallery ($valueId)
 
 insertGalleryValueInStore ($data)
 
 deleteGalleryValueInStore ($valueId, $entityId, $storeId)
 
 duplicate ($attributeId, $newFiles, $originalProductId, $newProductId)
 
 getProductImages ($product, $storeIds)
 
 countImageUses ($image)
 
- Public Member Functions inherited from AbstractDb
 __construct (\Magento\Framework\Model\ResourceModel\Db\Context $context, $connectionName=null)
 
 __sleep ()
 
 __wakeup ()
 
 getIdFieldName ()
 
 getMainTable ()
 
 getTable ($tableName)
 
 getConnection ()
 
 load (\Magento\Framework\Model\AbstractModel $object, $value, $field=null)
 
 save (\Magento\Framework\Model\AbstractModel $object)
 
 delete (\Magento\Framework\Model\AbstractModel $object)
 
 addUniqueField ($field)
 
 resetUniqueField ()
 
 unserializeFields (\Magento\Framework\Model\AbstractModel $object)
 
 getUniqueFields ()
 
 hasDataChanged ($object)
 
 getChecksum ($table)
 
 afterLoad (\Magento\Framework\DataObject $object)
 
 beforeSave (\Magento\Framework\DataObject $object)
 
 afterSave (\Magento\Framework\DataObject $object)
 
 beforeDelete (\Magento\Framework\DataObject $object)
 
 afterDelete (\Magento\Framework\DataObject $object)
 
 serializeFields (\Magento\Framework\Model\AbstractModel $object)
 
- Public Member Functions inherited from AbstractResource
 __construct ()
 
 getConnection ()
 
 beginTransaction ()
 
 addCommitCallback ($callback)
 
 commit ()
 
 rollBack ()
 
 getValidationRulesBeforeSave ()
 

Data Fields

const GALLERY_TABLE = 'catalog_product_entity_media_gallery'
 
const GALLERY_VALUE_TABLE = 'catalog_product_entity_media_gallery_value'
 
const GALLERY_VALUE_TO_ENTITY_TABLE = 'catalog_product_entity_media_gallery_value_to_entity'
 

Protected Member Functions

 _construct ()
 
 createBaseLoadSelect ($entityId, $storeId, $attributeId)
 
 removeDuplicates (&$result)
 
- Protected Member Functions inherited from AbstractDb
 _init ($mainTable, $idFieldName)
 
 _setResource ($connections, $tables=null)
 
 _setMainTable ($mainTable, $idFieldName=null)
 
 _getConnection ($resourceName)
 
 _getLoadSelect ($field, $value, $object)
 
 _initUniqueFields ()
 
 _prepareDataForSave (\Magento\Framework\Model\AbstractModel $object)
 
 _prepareValueForSave ($value, $type)
 
 _checkUnique (\Magento\Framework\Model\AbstractModel $object)
 
 _afterLoad (\Magento\Framework\Model\AbstractModel $object)
 
 _beforeSave (\Magento\Framework\Model\AbstractModel $object)
 
 _afterSave (\Magento\Framework\Model\AbstractModel $object)
 
 _beforeDelete (\Magento\Framework\Model\AbstractModel $object)
 
 _afterDelete (\Magento\Framework\Model\AbstractModel $object)
 
 _serializeFields (\Magento\Framework\Model\AbstractModel $object)
 
 prepareDataForUpdate ($object)
 
 isObjectNotNew (\Magento\Framework\Model\AbstractModel $object)
 
 saveNewObject (\Magento\Framework\Model\AbstractModel $object)
 
 updateObject (\Magento\Framework\Model\AbstractModel $object)
 
 processAfterSaves (\Magento\Framework\Model\AbstractModel $object)
 
 isModified (\Magento\Framework\Model\AbstractModel $object)
 
 processNotModifiedSave (\Magento\Framework\Model\AbstractModel $object)
 
- Protected Member Functions inherited from AbstractResource
 _construct ()
 
 _serializeField (DataObject $object, $field, $defaultValue=null, $unsetEmpty=false)
 
 _unserializeField (DataObject $object, $field, $defaultValue=null)
 
 _prepareDataForTable (DataObject $object, $table)
 
 _prepareTableValueForSave ($value, $type)
 
 _getColumnsForEntityLoad (\Magento\Framework\Model\AbstractModel $object, $tableName)
 
 getSerializer ()
 

Protected Attributes

 $metadata
 
- Protected Attributes inherited from AbstractDb
 $_resources
 
 $connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION
 
 $_connections = []
 
 $_resourceModel
 
 $_tables = []
 
 $_mainTable
 
 $_idFieldName
 
 $_isPkAutoIncrement = true
 
 $_useIsObjectNew = false
 
 $_mainTableFields
 
 $_uniqueFields = null
 
 $_serializableFields = []
 
 $transactionManager
 
 $objectRelationProcessor
 
- Protected Attributes inherited from AbstractResource
 $serializer
 
 $_logger
 

Detailed Description

Catalog product media gallery resource model.

@api

Since
101.0.0

Definition at line 16 of file Gallery.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Model\ResourceModel\Db\Context  $context,
\Magento\Framework\EntityManager\MetadataPool  $metadataPool,
  $connectionName = null 
)
Parameters
\Magento\Framework\Model\ResourceModel\Db\Context$context
\Magento\Framework\EntityManager\MetadataPool$metadataPool
string$connectionName

Definition at line 39 of file Gallery.php.

43  {
44  $this->metadata = $metadataPool->getMetadata(
45  \Magento\Catalog\Api\Data\ProductInterface::class
46  );
47 
48  parent::__construct($context, $connectionName);
49  }

Member Function Documentation

◆ _construct()

_construct ( )
protected

{}

Since
101.0.0

Definition at line 55 of file Gallery.php.

56  {
57  $this->_init(self::GALLERY_TABLE, 'value_id');
58  }

◆ bindValueToEntity()

bindValueToEntity (   $valueId,
  $entityId 
)
Parameters
int$valueId
int$entityId
Returns
int
Since
101.0.0

Definition at line 257 of file Gallery.php.

258  {
259  return $this->saveDataRow(
260  self::GALLERY_VALUE_TO_ENTITY_TABLE,
261  [
262  'value_id' => $valueId,
263  $this->metadata->getLinkField() => $entityId
264  ]
265  );
266  }

◆ countImageUses()

countImageUses (   $image)

Counts uses of this image.

Parameters
string$image
Returns
int
Since
101.0.8

Definition at line 482 of file Gallery.php.

483  {
484  $select = $this->getConnection()->select()
485  ->from([$this->getMainTableAlias() => $this->getMainTable()])
486  ->where(
487  'value = ?',
488  $image
489  );
490  return count($this->getConnection()->fetchAll($select));
491  }

◆ createBaseLoadSelect()

createBaseLoadSelect (   $entityId,
  $storeId,
  $attributeId 
)
protected
Parameters
int$entityId
int$storeId
int$attributeId
Returns
\Magento\Framework\DB\Select
Exceptions

Definition at line 142 of file Gallery.php.

143  {
144  $select = $this->createBatchBaseSelect($storeId, $attributeId);
145 
146  $select = $select->where(
147  'entity.' . $this->metadata->getLinkField() . ' = ?',
148  $entityId
149  );
150  return $select;
151  }

◆ createBatchBaseSelect()

createBatchBaseSelect (   $storeId,
  $attributeId 
)
Parameters
int$storeId
int$attributeId
Returns
\Magento\Framework\DB\Select
Exceptions

Definition at line 160 of file Gallery.php.

161  {
162  $linkField = $this->metadata->getLinkField();
163 
164  $positionCheckSql = $this->getConnection()->getCheckSql(
165  'value.position IS NULL',
166  'default_value.position',
167  'value.position'
168  );
169 
170  $mainTableAlias = $this->getMainTableAlias();
171 
172  $select = $this->getConnection()->select()->from(
173  [$mainTableAlias => $this->getMainTable()],
174  [
175  'value_id',
176  'file' => 'value',
177  'media_type'
178  ]
179  )->joinInner(
180  ['entity' => $this->getTable(self::GALLERY_VALUE_TO_ENTITY_TABLE)],
181  $mainTableAlias . '.value_id = entity.value_id',
182  [$linkField]
183  )->joinLeft(
184  ['value' => $this->getTable(self::GALLERY_VALUE_TABLE)],
185  implode(
186  ' AND ',
187  [
188  $mainTableAlias . '.value_id = value.value_id',
189  $this->getConnection()->quoteInto('value.store_id = ?', (int)$storeId),
190  'value.' . $linkField . ' = entity.' . $linkField,
191  ]
192  ),
193  ['label', 'position', 'disabled']
194  )->joinLeft(
195  ['default_value' => $this->getTable(self::GALLERY_VALUE_TABLE)],
196  implode(
197  ' AND ',
198  [
199  $mainTableAlias . '.value_id = default_value.value_id',
200  $this->getConnection()->quoteInto('default_value.store_id = ?', Store::DEFAULT_STORE_ID),
201  'default_value.' . $linkField . ' = entity.' . $linkField,
202  ]
203  ),
204  ['label_default' => 'label', 'position_default' => 'position', 'disabled_default' => 'disabled']
205  )->where(
206  $mainTableAlias . '.attribute_id = ?',
207  $attributeId
208  )->where(
209  $mainTableAlias . '.disabled = 0'
210  )->order(
211  $positionCheckSql . ' ' . \Magento\Framework\DB\Select::SQL_ASC
212  );
213 
214  return $select;
215  }
const SQL_ASC
Definition: Select.php:81

◆ deleteGallery()

deleteGallery (   $valueId)

Deletes gallery value in Db.

Parameters
array | integer$valueId
Returns
$this
Since
101.0.0

Definition at line 307 of file Gallery.php.

308  {
309  if (is_array($valueId) && count($valueId) > 0) {
310  $condition = $this->getConnection()->quoteInto('value_id IN(?) ', $valueId);
311  } elseif (!is_array($valueId)) {
312  $condition = $this->getConnection()->quoteInto('value_id = ? ', $valueId);
313  } else {
314  return $this;
315  }
316 
317  $this->getConnection()->delete($this->getMainTable(), $condition);
318 
319  return $this;
320  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ deleteGalleryValueInStore()

deleteGalleryValueInStore (   $valueId,
  $entityId,
  $storeId 
)

Deletes gallery value for store in DB.

Parameters
int$valueId
int$entityId
int$storeId
Returns
$this
Since
101.0.0

Definition at line 353 of file Gallery.php.

354  {
355  $conditions = implode(
356  ' AND ',
357  [
358  $this->getConnection()->quoteInto('value_id = ?', (int) $valueId),
359  $this->getConnection()->quoteInto($this->metadata->getLinkField() . ' = ?', (int) $entityId),
360  $this->getConnection()->quoteInto('store_id = ?', (int) $storeId)
361  ]
362  );
363 
364  $this->getConnection()->delete(
365  $this->getTable(self::GALLERY_VALUE_TABLE),
366  $conditions
367  );
368 
369  return $this;
370  }

◆ duplicate()

duplicate (   $attributeId,
  $newFiles,
  $originalProductId,
  $newProductId 
)

Duplicates gallery DB values.

Parameters
int$attributeId
array$newFiles
int$originalProductId
int$newProductId
Returns
array
Since
101.0.0

Definition at line 382 of file Gallery.php.

383  {
384  $linkField = $this->metadata->getLinkField();
385 
386  $select = $this->getConnection()->select()->from(
387  [$this->getMainTableAlias() => $this->getMainTable()],
388  ['value_id', 'value']
389  )->joinInner(
390  ['entity' => $this->getTable(self::GALLERY_VALUE_TO_ENTITY_TABLE)],
391  $this->getMainTableAlias() . '.value_id = entity.value_id',
392  [$linkField]
393  )->where(
394  'attribute_id = ?',
395  $attributeId
396  )->where(
397  'entity.' . $linkField . ' = ?',
398  $originalProductId
399  );
400 
401  $valueIdMap = [];
402 
403  // Duplicate main entries of gallery
404  foreach ($this->getConnection()->fetchAll($select) as $row) {
405  $data = [
406  'attribute_id' => $attributeId,
407  'value' => isset($newFiles[$row['value_id']]) ? $newFiles[$row['value_id']] : $row['value'],
408  ];
409 
410  $valueIdMap[$row['value_id']] = $this->insertGallery($data);
411  $this->bindValueToEntity($valueIdMap[$row['value_id']], $newProductId);
412  }
413 
414  if (count($valueIdMap) == 0) {
415  return [];
416  }
417 
418  // Duplicate per store gallery values
419  $select = $this->getConnection()->select()->from(
420  $this->getTable(self::GALLERY_VALUE_TABLE)
421  )->where(
422  'value_id IN(?)',
423  array_keys($valueIdMap)
424  );
425 
426  foreach ($this->getConnection()->fetchAll($select) as $row) {
427  unset($row['record_id']);
428 
429  $row[$linkField] = $newProductId;
430  $row['value_id'] = $valueIdMap[$row['value_id']];
431 
432  $this->insertGalleryValueInStore($row);
433  }
434 
435  return $valueIdMap;
436  }

◆ getConnection()

getConnection ( )

{}

Since
101.0.0

Definition at line 64 of file Gallery.php.

65  {
66  return $this->metadata->getEntityConnection();
67  }

◆ getMainTableAlias()

getMainTableAlias ( )
Returns
string
Since
101.0.0

Definition at line 246 of file Gallery.php.

247  {
248  return 'main';
249  }

◆ getProductImages()

getProductImages (   $product,
  $storeIds 
)

Returns product images in specific stores.

Parameters
\Magento\Catalog\Model\Product$product
int | array$storeIds
Returns
array
Since
101.0.0

Definition at line 446 of file Gallery.php.

447  {
448  if (!is_array($storeIds)) {
449  $storeIds = [$storeIds];
450  }
451 
452  $mainTable = $product->getResource()->getAttribute('image')->getBackend()->getTable();
453 
454  $select = $this->getConnection()->select()->from(
455  ['images' => $mainTable],
456  ['value as filepath', 'store_id']
457  )->joinLeft(
458  ['attr' => $this->getTable('eav_attribute')],
459  'images.attribute_id = attr.attribute_id',
460  ['attribute_code']
461  )->where(
462  $this->metadata->getLinkField() . ' = ?',
463  $product->getData($this->metadata->getLinkField())
464  )->where(
465  'store_id IN (?)',
466  $storeIds
467  )->where(
468  'attribute_code IN (?)',
469  ['small_image', 'thumbnail', 'image']
470  );
471 
472  return $this->getConnection()->fetchAll($select);
473  }

◆ insertGallery()

insertGallery (   $data)

Inserts gallery value to DB and retrieve last Id.

Parameters
array$data
Returns
int
Since
101.0.0

Definition at line 288 of file Gallery.php.

289  {
290  $data = $this->_prepareDataForTable(
291  new \Magento\Framework\DataObject($data),
292  $this->getMainTable()
293  );
294 
295  $this->getConnection()->insert($this->getMainTable(), $data);
296 
297  return $this->getConnection()->lastInsertId($this->getMainTable());
298  }

◆ insertGalleryValueInStore()

insertGalleryValueInStore (   $data)

Inserts gallery value for store to Db.

Parameters
array$data
Returns
$this
Since
101.0.0

Definition at line 329 of file Gallery.php.

330  {
331  $data = $this->_prepareDataForTable(
332  new \Magento\Framework\DataObject($data),
333  $this->getTable(self::GALLERY_VALUE_TABLE)
334  );
335 
336  $this->getConnection()->insert(
337  $this->getTable(self::GALLERY_VALUE_TABLE),
338  $data
339  );
340 
341  return $this;
342  }

◆ loadDataFromTableByValueId()

loadDataFromTableByValueId (   $tableNameAlias,
array  $ids,
  $storeId = null,
array  $cols = null,
array  $leftJoinTables = [],
  $whereCondition = null 
)
Parameters
string$tableNameAlias
array$ids
int | null$storeId
array | null$cols
array$leftJoinTables
string$whereCondition
Returns
array
Since
101.0.0

Definition at line 79 of file Gallery.php.

86  {
87  if (null == $cols) {
88  $cols = '*';
89  }
90  $mainTableAlias = $this->getMainTableAlias();
91  $select = $this->getConnection()->select()
92  ->from(
93  [$mainTableAlias => $this->getTable($tableNameAlias)],
94  $cols
95  )->where(
96  $mainTableAlias . '.value_id IN(?)',
97  $ids
98  );
99  if (null !== $storeId) {
100  $select->where($mainTableAlias . '.store_id = ?', $storeId);
101  }
102  if (null !== $whereCondition) {
103  $select->where($whereCondition);
104  }
105  foreach ($leftJoinTables as $joinParameters) {
106  $select->joinLeft($joinParameters[0], $joinParameters[1], $joinParameters[2]);
107  }
108  $result = $this->getConnection()->fetchAll($select);
109 
110  return $result;
111  }

◆ loadProductGalleryByAttributeId()

loadProductGalleryByAttributeId (   $product,
  $attributeId 
)
Parameters
\Magento\Catalog\Model\Product$product
int$attributeId
Returns
array
Since
101.0.0

Definition at line 119 of file Gallery.php.

120  {
121  $select = $this->createBaseLoadSelect(
122  $product->getData($this->metadata->getLinkField()),
123  $product->getStoreId(),
124  $attributeId
125  );
126 
127  $result = $this->getConnection()->fetchAll($select);
128 
129  $this->removeDuplicates($result);
130 
131  return $result;
132  }

◆ removeDuplicates()

removeDuplicates ( $result)
protected

Removes duplicates.

Parameters
array&$result
Returns
$this
Since
101.0.0

Definition at line 224 of file Gallery.php.

225  {
226  $fileToId = [];
227 
228  foreach (array_keys($result) as $index) {
229  if (!isset($fileToId[$result[$index]['file']])) {
230  $fileToId[$result[$index]['file']] = $result[$index]['value_id'];
231  } elseif ($fileToId[$result[$index]['file']] != $result[$index]['value_id']) {
232  $this->deleteGallery($result[$index]['value_id']);
233  unset($result[$index]);
234  }
235  }
236 
237  $result = array_values($result);
238 
239  return $this;
240  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$index
Definition: list.phtml:44

◆ saveDataRow()

saveDataRow (   $table,
array  $data,
array  $fields = [] 
)
Parameters
string$table
array$data
array$fields
Returns
int
Since
101.0.0

Definition at line 275 of file Gallery.php.

276  {
277  $table = $this->getTable($table);
278  return $this->getConnection()->insertOnDuplicate($table, $data, $fields);
279  }
$fields
Definition: details.phtml:14
$table
Definition: trigger.php:14

Field Documentation

◆ $metadata

$metadata
protected

Definition at line 32 of file Gallery.php.

◆ GALLERY_TABLE

const GALLERY_TABLE = 'catalog_product_entity_media_gallery'

#+ Constants defined for keys of data array

Definition at line 21 of file Gallery.php.

◆ GALLERY_VALUE_TABLE

const GALLERY_VALUE_TABLE = 'catalog_product_entity_media_gallery_value'

Definition at line 23 of file Gallery.php.

◆ GALLERY_VALUE_TO_ENTITY_TABLE

const GALLERY_VALUE_TO_ENTITY_TABLE = 'catalog_product_entity_media_gallery_value_to_entity'

Definition at line 25 of file Gallery.php.


The documentation for this class was generated from the following file: