Skip to content

[Backport] Don't load product collection in review observer #23094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions app/code/Magento/Review/Block/Customer/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Review\Block\Customer;

use Magento\Catalog\Model\Product;
Expand Down Expand Up @@ -91,6 +92,7 @@ public function __construct(
* Initialize review id
*
* @return void
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
*/
protected function _construct()
{
Expand Down Expand Up @@ -160,6 +162,7 @@ public function getRating()
/**
* Get rating summary
*
* @deprecated
* @return array
*/
public function getRatingSummary()
Expand Down Expand Up @@ -201,26 +204,7 @@ public function dateFormat($date)
}

/**
* Get product reviews summary
*
* @param \Magento\Catalog\Model\Product $product
* @param bool $templateType
* @param bool $displayIfNoReviews
* @return string
*/
public function getReviewsSummaryHtml(
\Magento\Catalog\Model\Product $product,
$templateType = false,
$displayIfNoReviews = false
) {
if (!$product->getRatingSummary()) {
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
}
return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
}

/**
* @return string
* @inheritDoc
*/
protected function _toHtml()
{
Expand Down

This file was deleted.

31 changes: 24 additions & 7 deletions app/code/Magento/Review/Block/Product/ReviewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Review\Block\Product;

use Magento\Catalog\Block\Product\ReviewRendererInterface;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Review\Model\ReviewSummaryFactory;
use Magento\Review\Observer\PredispatchReviewObserver;

/**
Expand All @@ -33,17 +36,26 @@ class ReviewRenderer extends \Magento\Framework\View\Element\Template implements
*/
protected $_reviewFactory;

/**
* @var ReviewSummaryFactory
*/
private $reviewSummaryFactory;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Review\Model\ReviewFactory $reviewFactory
* @param array $data
* @param ReviewSummaryFactory $reviewSummaryFactory
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Review\Model\ReviewFactory $reviewFactory,
array $data = []
array $data = [],
ReviewSummaryFactory $reviewSummaryFactory = null
) {
$this->_reviewFactory = $reviewFactory;
$this->reviewSummaryFactory = $reviewSummaryFactory ??
ObjectManager::getInstance()->get(ReviewSummaryFactory::class);
parent::__construct($context, $data);
}

Expand All @@ -52,7 +64,7 @@ public function __construct(
*
* @return string
*/
public function isReviewEnabled() : string
public function isReviewEnabled(): string
{
return $this->_scopeConfig->getValue(
PredispatchReviewObserver::XML_PATH_REVIEW_ACTIVE,
Expand All @@ -68,17 +80,22 @@ public function isReviewEnabled() : string
* @param bool $displayIfNoReviews
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getReviewsSummaryHtml(
\Magento\Catalog\Model\Product $product,
$templateType = self::DEFAULT_VIEW,
$displayIfNoReviews = false
) {
if (!$product->getRatingSummary()) {
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
if ($product->getRatingSummary() === null) {
$this->reviewSummaryFactory->create()->appendSummaryDataToObject(
$product,
$this->_storeManager->getStore()->getId()
);
}

if (!$product->getRatingSummary() && !$displayIfNoReviews) {
if (null === $product->getRatingSummary() && !$displayIfNoReviews) {
return '';
}
// pick template among available
Expand All @@ -101,7 +118,7 @@ public function getReviewsSummaryHtml(
*/
public function getRatingSummary()
{
return $this->getProduct()->getRatingSummary()->getRatingSummary();
return $this->getProduct()->getRatingSummary();
}

/**
Expand All @@ -111,7 +128,7 @@ public function getRatingSummary()
*/
public function getReviewsCount()
{
return $this->getProduct()->getRatingSummary()->getReviewsCount();
return $this->getProduct()->getReviewsCount();
}

/**
Expand Down
20 changes: 1 addition & 19 deletions app/code/Magento/Review/Block/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public function getRating()
/**
* Retrieve rating summary for current product
*
* @deprecated
* @return string
*/
public function getRatingSummary()
Expand Down Expand Up @@ -160,23 +161,4 @@ public function dateFormat($date)
{
return $this->formatDate($date, \IntlDateFormatter::LONG);
}

/**
* Get product reviews summary
*
* @param \Magento\Catalog\Model\Product $product
* @param bool $templateType
* @param bool $displayIfNoReviews
* @return string
*/
public function getReviewsSummaryHtml(
\Magento\Catalog\Model\Product $product,
$templateType = false,
$displayIfNoReviews = false
) {
if (!$product->getRatingSummary()) {
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
}
return parent::getReviewsSummaryHtml($product, $templateType, $displayIfNoReviews);
}
}
43 changes: 43 additions & 0 deletions app/code/Magento/Review/Model/ResourceModel/Review/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Review\Model\ResourceModel\Review;

use Magento\Framework\Model\AbstractModel;
Expand Down Expand Up @@ -73,4 +74,46 @@ public function reAggregate($summary)
}
return $this;
}

/**
* Append review summary fields to product collection
*
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection
* @param string $storeId
* @param string $entityCode
* @return Summary
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function appendSummaryFieldsToCollection(
\Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection,
string $storeId,
string $entityCode
) {
if (!$productCollection->isLoaded()) {
$summaryEntitySubSelect = $this->getConnection()->select();
$summaryEntitySubSelect
->from(
['review_entity' => $this->getTable('review_entity')],
['entity_id']
)->where(
'entity_code = ?',
$entityCode
);
$joinCond = new \Zend_Db_Expr(
"e.entity_id = review_summary.entity_pk_value AND review_summary.store_id = {$storeId}"
. " AND review_summary.entity_type = ({$summaryEntitySubSelect})"
);
$productCollection->getSelect()
->joinLeft(
['review_summary' => $this->getMainTable()],
$joinCond,
[
'reviews_count' => new \Zend_Db_Expr("IFNULL(review_summary.reviews_count, 0)"),
'rating_summary' => new \Zend_Db_Expr("IFNULL(review_summary.rating_summary, 0)")
]
);
}

return $this;
}
}
11 changes: 8 additions & 3 deletions app/code/Magento/Review/Model/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Review\Model;

use Magento\Framework\DataObject;
Expand Down Expand Up @@ -100,6 +101,7 @@ class Review extends \Magento\Framework\Model\AbstractModel implements IdentityI
/**
* Review model summary
*
* @deprecated Summary factory injected as separate property
* @var \Magento\Review\Model\Review\Summary
*/
protected $_reviewSummary;
Expand Down Expand Up @@ -214,6 +216,7 @@ public function aggregate()
/**
* Get entity summary
*
* @deprecated
* @param Product $product
* @param int $storeId
* @return void
Expand Down Expand Up @@ -301,10 +304,12 @@ public function afterDeleteCommit()
}

/**
* Append review summary to product collection
* Append review summary data object to product collection
*
* @deprecated
* @param ProductCollection $collection
* @return $this
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function appendSummary($collection)
{
Expand All @@ -313,7 +318,7 @@ public function appendSummary($collection)
$entityIds[] = $item->getEntityId();
}

if (sizeof($entityIds) == 0) {
if (count($entityIds) === 0) {
return $this;
}

Expand Down Expand Up @@ -356,7 +361,7 @@ public function isAvailableOnStore($store = null)
{
$store = $this->_storeManager->getStore($store);
if ($store) {
return in_array($store->getId(), (array) $this->getStores());
return in_array($store->getId(), (array)$this->getStores());
}
return false;
}
Expand Down
Loading