Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Sample.php
Go to the documentation of this file.
1 <?php
7 
9 
17 {
22  protected $metadataPool;
23 
29  public function __construct(
30  \Magento\Framework\Model\ResourceModel\Db\Context $context,
31  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
32  $connectionName = null
33  ) {
34  $this->metadataPool = $metadataPool;
35  parent::__construct($context, $connectionName);
36  }
37 
43  protected function _construct()
44  {
45  $this->_init('downloadable_sample', 'sample_id');
46  }
47 
54  public function saveItemTitle($sampleObject)
55  {
56  $connection = $this->getConnection();
57  $sampleTitleTable = $this->getTable('downloadable_sample_title');
58  $bind = [':sample_id' => $sampleObject->getId(), ':store_id' => (int)$sampleObject->getStoreId()];
59  $select = $connection->select()->from(
60  $sampleTitleTable
61  )->where(
62  'sample_id=:sample_id AND store_id=:store_id'
63  );
64  if ($connection->fetchOne($select, $bind)) {
65  $where = [
66  'sample_id = ?' => $sampleObject->getId(),
67  'store_id = ?' => (int)$sampleObject->getStoreId(),
68  ];
69  if ($sampleObject->getUseDefaultTitle()) {
70  $connection->delete($sampleTitleTable, $where);
71  } else {
72  $connection->update($sampleTitleTable, ['title' => $sampleObject->getTitle()], $where);
73  }
74  } else {
75  if (!$sampleObject->getUseDefaultTitle()) {
76  $connection->insert(
77  $sampleTitleTable,
78  [
79  'sample_id' => $sampleObject->getId(),
80  'store_id' => (int)$sampleObject->getStoreId(),
81  'title' => $sampleObject->getTitle()
82  ]
83  );
84  }
85  }
86  return $this;
87  }
88 
95  public function deleteItems($items)
96  {
97  $connection = $this->getConnection();
98  if ($items instanceof \Magento\Downloadable\Model\Sample) {
99  $where = ['sample_id = ?' => $items->getId()];
100  } else {
101  $where = ['sample_id in (?)' => $items];
102  }
103  $connection->delete($this->getMainTable(), $where);
104  $connection->delete($this->getTable('downloadable_sample_title'), $where);
105  return $this;
106  }
107 
116  {
117  $connection = $this->getConnection();
118  $ifNullDefaultTitle = $connection->getIfNullSql('st.title', 'd.title');
119  $select = $connection->select()->from(
120  ['m' => $this->getMainTable()],
121  null
122  )->join(
123  ['d' => $this->getTable('downloadable_sample_title')],
124  'd.sample_id=m.sample_id AND d.store_id=0',
125  []
126  )->join(
127  ['cpe' => $this->getTable('catalog_product_entity')],
128  sprintf(
129  'cpe.entity_id = m.product_id',
130  $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()
131  ),
132  []
133  )->joinLeft(
134  ['st' => $this->getTable('downloadable_sample_title')],
135  'st.sample_id=m.sample_id AND st.store_id=:store_id',
136  ['title' => $ifNullDefaultTitle]
137  )->where(
138  'cpe.entity_id=:product_id',
139  $productId
140  );
141  $bind = [':store_id' => (int)$storeId, ':product_id' => $productId];
142 
143  return $connection->fetchCol($select, $bind);
144  }
145 }
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\EntityManager\MetadataPool $metadataPool, $connectionName=null)
Definition: Sample.php:29
$connection
Definition: bulk.php:13
$items