Skip to content

Commit 016bba0

Browse files
committed
Merge remote-tracking branch 'mainline/2.3-develop' into 2.3-develop-pr24
2 parents 3fdde60 + 4dac65b commit 016bba0

File tree

14 files changed

+397
-79
lines changed

14 files changed

+397
-79
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,11 @@ protected function initializeProductData(array $productData, $createNew)
369369
if ($createNew) {
370370
$product = $this->productFactory->create();
371371
$this->assignProductToWebsites($product);
372+
} elseif (!empty($productData['id'])) {
373+
$this->removeProductFromLocalCacheById($productData['id']);
374+
$product = $this->getById($productData['id']);
372375
} else {
373-
$this->removeProductFromLocalCache($productData['sku']);
376+
$this->removeProductFromLocalCacheBySku($productData['sku']);
374377
$product = $this->get($productData['sku']);
375378
}
376379

@@ -512,7 +515,7 @@ public function save(ProductInterface $product, $saveOptions = false)
512515
$tierPrices = $product->getData('tier_price');
513516

514517
try {
515-
$existingProduct = $this->get($product->getSku());
518+
$existingProduct = $product->getId() ? $this->getById($product->getId()) : $this->get($product->getSku());
516519

517520
$product->setData(
518521
$this->resourceModel->getLinkField(),
@@ -570,8 +573,8 @@ public function save(ProductInterface $product, $saveOptions = false)
570573
$product->getCategoryIds()
571574
);
572575
}
573-
$this->removeProductFromLocalCache($product->getSku());
574-
unset($this->instancesById[$product->getId()]);
576+
$this->removeProductFromLocalCacheBySku($product->getSku());
577+
$this->removeProductFromLocalCacheById($product->getId());
575578

576579
return $this->get($product->getSku(), false, $product->getStoreId());
577580
}
@@ -584,8 +587,8 @@ public function delete(ProductInterface $product)
584587
$sku = $product->getSku();
585588
$productId = $product->getId();
586589
try {
587-
$this->removeProductFromLocalCache($product->getSku());
588-
unset($this->instancesById[$product->getId()]);
590+
$this->removeProductFromLocalCacheBySku($product->getSku());
591+
$this->removeProductFromLocalCacheById($product->getId());
589592
$this->resourceModel->delete($product);
590593
} catch (ValidatorException $e) {
591594
throw new CouldNotSaveException(__($e->getMessage()), $e);
@@ -595,8 +598,8 @@ public function delete(ProductInterface $product)
595598
$e
596599
);
597600
}
598-
$this->removeProductFromLocalCache($sku);
599-
unset($this->instancesById[$productId]);
601+
$this->removeProductFromLocalCacheBySku($sku);
602+
$this->removeProductFromLocalCacheById($productId);
600603

601604
return true;
602605
}
@@ -753,25 +756,36 @@ private function getProductFromLocalCache(string $sku, string $cacheKey)
753756
}
754757

755758
/**
756-
* Removes product in the local cache.
759+
* Removes product in the local cache by sku.
757760
*
758761
* @param string $sku
759762
* @return void
760763
*/
761-
private function removeProductFromLocalCache(string $sku) :void
764+
private function removeProductFromLocalCacheBySku(string $sku): void
762765
{
763766
$preparedSku = $this->prepareSku($sku);
764767
unset($this->instances[$preparedSku]);
765768
}
766769

767770
/**
768-
* Saves product in the local cache.
771+
* Removes product in the local cache by id.
772+
*
773+
* @param string|null $id
774+
* @return void
775+
*/
776+
private function removeProductFromLocalCacheById(?string $id): void
777+
{
778+
unset($this->instancesById[$id]);
779+
}
780+
781+
/**
782+
* Saves product in the local cache by sku.
769783
*
770784
* @param Product $product
771785
* @param string $cacheKey
772786
* @return void
773787
*/
774-
private function saveProductInLocalCache(Product $product, string $cacheKey) : void
788+
private function saveProductInLocalCache(Product $product, string $cacheKey): void
775789
{
776790
$preparedSku = $this->prepareSku($product->getSku());
777791
$this->instances[$preparedSku][$cacheKey] = $product;
@@ -800,8 +814,8 @@ private function prepareSku(string $sku): string
800814
private function saveProduct($product): void
801815
{
802816
try {
803-
$this->removeProductFromLocalCache($product->getSku());
804-
unset($this->instancesById[$product->getId()]);
817+
$this->removeProductFromLocalCacheBySku($product->getSku());
818+
$this->removeProductFromLocalCacheById($product->getId());
805819
$this->resourceModel->save($product);
806820
} catch (ConnectionException $exception) {
807821
throw new TemporaryCouldNotSaveException(

app/code/Magento/Catalog/Model/ResourceModel/Product/Image.php

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(
5454
}
5555

5656
/**
57-
* Returns product images
57+
* Get all product images.
5858
*
5959
* @return \Generator
6060
*/
@@ -75,7 +75,28 @@ public function getAllProductImages(): \Generator
7575
}
7676

7777
/**
78-
* Get the number of unique pictures of products
78+
* Get used product images.
79+
*
80+
* @return \Generator
81+
*/
82+
public function getUsedProductImages(): \Generator
83+
{
84+
$batchSelectIterator = $this->batchQueryGenerator->generate(
85+
'value_id',
86+
$this->getUsedImagesSelect(),
87+
$this->batchSize,
88+
\Magento\Framework\DB\Query\BatchIteratorInterface::NON_UNIQUE_FIELD_ITERATOR
89+
);
90+
91+
foreach ($batchSelectIterator as $select) {
92+
foreach ($this->connection->fetchAll($select) as $key => $value) {
93+
yield $key => $value;
94+
}
95+
}
96+
}
97+
98+
/**
99+
* Get the number of unique images of products.
79100
*
80101
* @return int
81102
*/
@@ -92,7 +113,24 @@ public function getCountAllProductImages(): int
92113
}
93114

94115
/**
95-
* Return Select to fetch all products images
116+
* Get the number of unique and used images of products.
117+
*
118+
* @return int
119+
*/
120+
public function getCountUsedProductImages(): int
121+
{
122+
$select = $this->getUsedImagesSelect()
123+
->reset('columns')
124+
->reset('distinct')
125+
->columns(
126+
new \Zend_Db_Expr('count(distinct value)')
127+
);
128+
129+
return (int) $this->connection->fetchOne($select);
130+
}
131+
132+
/**
133+
* Return select to fetch all products images.
96134
*
97135
* @return Select
98136
*/
@@ -106,4 +144,23 @@ private function getVisibleImagesSelect(): Select
106144
'disabled = 0'
107145
);
108146
}
147+
148+
/**
149+
* Return select to fetch all used product images.
150+
*
151+
* @return Select
152+
*/
153+
private function getUsedImagesSelect(): Select
154+
{
155+
return $this->connection->select()->distinct()
156+
->from(
157+
['images' => $this->resourceConnection->getTableName(Gallery::GALLERY_TABLE)],
158+
'value as filepath'
159+
)->joinInner(
160+
['image_value' => $this->resourceConnection->getTableName(Gallery::GALLERY_VALUE_TABLE)],
161+
'images.value_id = image_value.value_id'
162+
)->where(
163+
'images.disabled = 0 AND image_value.disabled = 0'
164+
);
165+
}
109166
}

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,12 +527,14 @@ private function getProductMocksForReducedCache($productsCount)
527527
for ($i = 1; $i <= $productsCount; $i++) {
528528
$productMock = $this->getMockBuilder(Product::class)
529529
->disableOriginalConstructor()
530-
->setMethods([
530+
->setMethods(
531+
[
531532
'getId',
532533
'getSku',
533534
'load',
534535
'setData',
535-
])
536+
]
537+
)
536538
->getMock();
537539
$productMock->expects($this->once())->method('load');
538540
$productMock->expects($this->atLeastOnce())->method('getId')->willReturn($i);
@@ -679,7 +681,7 @@ public function testSaveException()
679681
->willReturn(true);
680682
$this->resourceModel->expects($this->once())->method('save')->with($this->product)
681683
->willThrowException(new \Magento\Eav\Model\Entity\Attribute\Exception(__('123')));
682-
$this->product->expects($this->once())->method('getId')->willReturn(null);
684+
$this->product->expects($this->exactly(2))->method('getId')->willReturn(null);
683685
$this->extensibleDataObjectConverter
684686
->expects($this->once())
685687
->method('toNestedArray')
@@ -703,7 +705,7 @@ public function testSaveInvalidProductException()
703705
$this->initializationHelper->expects($this->never())->method('initialize');
704706
$this->resourceModel->expects($this->once())->method('validate')->with($this->product)
705707
->willReturn(['error1', 'error2']);
706-
$this->product->expects($this->never())->method('getId');
708+
$this->product->expects($this->once())->method('getId')->willReturn(null);
707709
$this->extensibleDataObjectConverter
708710
->expects($this->once())
709711
->method('toNestedArray')
@@ -1340,11 +1342,13 @@ public function testSaveWithDifferentWebsites()
13401342
->willReturn($storeMock);
13411343
$this->storeManager->expects($this->once())
13421344
->method('getWebsites')
1343-
->willReturn([
1345+
->willReturn(
1346+
[
13441347
1 => ['first'],
13451348
2 => ['second'],
13461349
3 => ['third']
1347-
]);
1350+
]
1351+
);
13481352
$this->product->expects($this->once())->method('setWebsiteIds')->willReturn([2,3]);
13491353
$this->product->method('getSku')->willReturn('simple');
13501354

0 commit comments

Comments
 (0)