Summary.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Model\ResourceModel\Review;
  7. use Magento\Framework\Model\AbstractModel;
  8. /**
  9. * Review summary resource model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. class Summary extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Define module
  17. *
  18. * @return void
  19. */
  20. protected function _construct()
  21. {
  22. $this->_init('review_entity_summary', 'entity_pk_value');
  23. }
  24. /**
  25. * Retrieve select object for load object data
  26. *
  27. * @param string $field
  28. * @param mixed $value
  29. * @param AbstractModel $object
  30. * @return \Magento\Framework\DB\Select
  31. */
  32. protected function _getLoadSelect($field, $value, $object)
  33. {
  34. $select = parent::_getLoadSelect($field, $value, $object);
  35. $select->where('store_id = ?', (int)$object->getStoreId());
  36. return $select;
  37. }
  38. /**
  39. * Re-aggregate all data by rating summary
  40. *
  41. * @param array $summary
  42. * @return $this
  43. */
  44. public function reAggregate($summary)
  45. {
  46. $connection = $this->getConnection();
  47. $select = $connection->select()->from(
  48. $this->getMainTable(),
  49. ['primary_id' => new \Zend_Db_Expr('MAX(primary_id)'), 'store_id', 'entity_pk_value']
  50. )->group(
  51. ['entity_pk_value', 'store_id']
  52. );
  53. foreach ($connection->fetchAll($select) as $row) {
  54. if (isset($summary[$row['store_id']]) && isset($summary[$row['store_id']][$row['entity_pk_value']])) {
  55. $summaryItem = $summary[$row['store_id']][$row['entity_pk_value']];
  56. if ($summaryItem->getCount()) {
  57. $ratingSummary = round($summaryItem->getSum() / $summaryItem->getCount());
  58. } else {
  59. $ratingSummary = $summaryItem->getSum();
  60. }
  61. } else {
  62. $ratingSummary = 0;
  63. }
  64. $connection->update(
  65. $this->getMainTable(),
  66. ['rating_summary' => $ratingSummary],
  67. $connection->quoteInto('primary_id = ?', $row['primary_id'])
  68. );
  69. }
  70. return $this;
  71. }
  72. }