Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PricePersistence.php
Go to the documentation of this file.
1 <?php
8 
13 {
19  private $table = 'catalog_product_entity_decimal';
20 
24  private $attributeResource;
25 
29  private $attributeRepository;
30 
34  private $productIdLocator;
35 
41  private $metadataPool;
42 
48  private $attributeCode;
49 
55  private $attributeId;
56 
62  private $itemsPerOperation = 500;
63 
73  public function __construct(
74  \Magento\Catalog\Model\ResourceModel\Attribute $attributeResource,
75  \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository,
76  \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator,
77  \Magento\Framework\EntityManager\MetadataPool $metadataPool,
78  $attributeCode = ''
79  ) {
80  $this->attributeResource = $attributeResource;
81  $this->attributeRepository = $attributeRepository;
82  $this->attributeCode = $attributeCode;
83  $this->productIdLocator = $productIdLocator;
84  $this->metadataPool = $metadataPool;
85  }
86 
93  public function get(array $skus)
94  {
95  $ids = $this->retrieveAffectedIds($skus);
96  $select = $this->attributeResource->getConnection()
97  ->select()
98  ->from($this->attributeResource->getTable($this->table));
99  return $this->attributeResource->getConnection()->fetchAll(
100  $select->where($this->getEntityLinkField() . ' IN (?)', $ids)
101  ->where('attribute_id = ?', $this->getAttributeId())
102  );
103  }
104 
112  public function update(array $prices)
113  {
114  array_walk($prices, function (&$price) {
115  return $price['attribute_id'] = $this->getAttributeId();
116  });
117  $connection = $this->attributeResource->getConnection();
118  $connection->beginTransaction();
119  try {
120  foreach (array_chunk($prices, $this->itemsPerOperation) as $pricesBunch) {
121  $this->attributeResource->getConnection()->insertOnDuplicate(
122  $this->attributeResource->getTable($this->table),
123  $pricesBunch,
124  ['value']
125  );
126  }
127  $connection->commit();
128  } catch (\Exception $e) {
129  $connection->rollBack();
130  throw new \Magento\Framework\Exception\CouldNotSaveException(
131  __('Could not save Prices.'),
132  $e
133  );
134  }
135  }
136 
144  public function delete(array $skus)
145  {
146  $ids = $this->retrieveAffectedIds($skus);
147  $connection = $this->attributeResource->getConnection();
148  $connection->beginTransaction();
149  try {
150  foreach (array_chunk($ids, $this->itemsPerOperation) as $idsBunch) {
151  $this->attributeResource->getConnection()->delete(
152  $this->attributeResource->getTable($this->table),
153  [
154  'attribute_id = ?' => $this->getAttributeId(),
155  $this->getEntityLinkField() . ' IN (?)' => $idsBunch
156  ]
157  );
158  }
159  $connection->commit();
160  } catch (\Exception $e) {
161  $connection->rollBack();
162  throw new \Magento\Framework\Exception\CouldNotDeleteException(
163  __('Could not delete Prices'),
164  $e
165  );
166  }
167  }
168 
176  public function retrieveSkuById($id, $skus)
177  {
178  foreach ($this->productIdLocator->retrieveProductIdsBySkus($skus) as $sku => $ids) {
179  if (false !== array_key_exists($id, $ids)) {
180  return $sku;
181  }
182  }
183 
184  return null;
185  }
186 
192  private function getAttributeId()
193  {
194  if (!$this->attributeId) {
195  $this->attributeId = $this->attributeRepository->get($this->attributeCode)->getAttributeId();
196  }
197 
198  return $this->attributeId;
199  }
200 
207  private function retrieveAffectedIds(array $skus)
208  {
209  $affectedIds = [];
210 
211  foreach ($this->productIdLocator->retrieveProductIdsBySkus($skus) as $productIds) {
212  $affectedIds = array_merge($affectedIds, array_keys($productIds));
213  }
214 
215  return array_unique($affectedIds);
216  }
217 
223  public function getEntityLinkField()
224  {
225  return $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
226  ->getLinkField();
227  }
228 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$price
foreach($websiteCodes as $websiteCode) $skus
__construct(\Magento\Catalog\Model\ResourceModel\Attribute $attributeResource, \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository, \Magento\Catalog\Model\ProductIdLocatorInterface $productIdLocator, \Magento\Framework\EntityManager\MetadataPool $metadataPool, $attributeCode='')
$connection
Definition: bulk.php:13