Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Rating Class Reference
Inheritance diagram for Rating:
AbstractDb AbstractResource

Public Member Functions

 __construct (\Magento\Framework\Model\ResourceModel\Db\Context $context, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Module\Manager $moduleManager, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Review\Model\ResourceModel\Review\Summary $reviewSummary, $connectionName=null)
 
 getStores ($ratingId)
 
 getEntitySummary ($object, $onlyForCurrentStore=true)
 
 getReviewSummary ($object, $onlyForCurrentStore=true)
 
 getEntityIdByCode ($entityCode)
 
 deleteAggregatedRatingsByProductId ($productId)
 
- Public Member Functions inherited from AbstractDb
 __construct (\Magento\Framework\Model\ResourceModel\Db\Context $context, $connectionName=null)
 
 __sleep ()
 
 __wakeup ()
 
 getIdFieldName ()
 
 getMainTable ()
 
 getTable ($tableName)
 
 getConnection ()
 
 load (\Magento\Framework\Model\AbstractModel $object, $value, $field=null)
 
 save (\Magento\Framework\Model\AbstractModel $object)
 
 delete (\Magento\Framework\Model\AbstractModel $object)
 
 addUniqueField ($field)
 
 resetUniqueField ()
 
 unserializeFields (\Magento\Framework\Model\AbstractModel $object)
 
 getUniqueFields ()
 
 hasDataChanged ($object)
 
 getChecksum ($table)
 
 afterLoad (\Magento\Framework\DataObject $object)
 
 beforeSave (\Magento\Framework\DataObject $object)
 
 afterSave (\Magento\Framework\DataObject $object)
 
 beforeDelete (\Magento\Framework\DataObject $object)
 
 afterDelete (\Magento\Framework\DataObject $object)
 
 serializeFields (\Magento\Framework\Model\AbstractModel $object)
 
- Public Member Functions inherited from AbstractResource
 __construct ()
 
 getConnection ()
 
 beginTransaction ()
 
 addCommitCallback ($callback)
 
 commit ()
 
 rollBack ()
 
 getValidationRulesBeforeSave ()
 

Data Fields

const RATING_STATUS_APPROVED = 'Approved'
 

Protected Member Functions

 _construct ()
 
 _initUniqueFields ()
 
 _getLoadSelect ($field, $value, $object)
 
 _afterLoad (\Magento\Framework\Model\AbstractModel $object)
 
 _afterSave (\Magento\Framework\Model\AbstractModel $object)
 
 processRatingCodes (\Magento\Framework\Model\AbstractModel $object)
 
 processRatingStores (\Magento\Framework\Model\AbstractModel $object)
 
 deleteRatingData ($ratingId, $table, array $storeIds)
 
 insertRatingData ($table, array $data)
 
 _afterDelete (\Magento\Framework\Model\AbstractModel $object)
 
 _getEntitySummaryData ($object)
 
- Protected Member Functions inherited from AbstractDb
 _init ($mainTable, $idFieldName)
 
 _setResource ($connections, $tables=null)
 
 _setMainTable ($mainTable, $idFieldName=null)
 
 _getConnection ($resourceName)
 
 _getLoadSelect ($field, $value, $object)
 
 _initUniqueFields ()
 
 _prepareDataForSave (\Magento\Framework\Model\AbstractModel $object)
 
 _prepareValueForSave ($value, $type)
 
 _checkUnique (\Magento\Framework\Model\AbstractModel $object)
 
 _afterLoad (\Magento\Framework\Model\AbstractModel $object)
 
 _beforeSave (\Magento\Framework\Model\AbstractModel $object)
 
 _afterSave (\Magento\Framework\Model\AbstractModel $object)
 
 _beforeDelete (\Magento\Framework\Model\AbstractModel $object)
 
 _afterDelete (\Magento\Framework\Model\AbstractModel $object)
 
 _serializeFields (\Magento\Framework\Model\AbstractModel $object)
 
 prepareDataForUpdate ($object)
 
 isObjectNotNew (\Magento\Framework\Model\AbstractModel $object)
 
 saveNewObject (\Magento\Framework\Model\AbstractModel $object)
 
 updateObject (\Magento\Framework\Model\AbstractModel $object)
 
 processAfterSaves (\Magento\Framework\Model\AbstractModel $object)
 
 isModified (\Magento\Framework\Model\AbstractModel $object)
 
 processNotModifiedSave (\Magento\Framework\Model\AbstractModel $object)
 
- Protected Member Functions inherited from AbstractResource
 _construct ()
 
 _serializeField (DataObject $object, $field, $defaultValue=null, $unsetEmpty=false)
 
 _unserializeField (DataObject $object, $field, $defaultValue=null)
 
 _prepareDataForTable (DataObject $object, $table)
 
 _prepareTableValueForSave ($value, $type)
 
 _getColumnsForEntityLoad (\Magento\Framework\Model\AbstractModel $object, $tableName)
 
 getSerializer ()
 

Protected Attributes

 $_storeManager
 
 $moduleManager
 
 $_logger
 
- Protected Attributes inherited from AbstractDb
 $_resources
 
 $connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION
 
 $_connections = []
 
 $_resourceModel
 
 $_tables = []
 
 $_mainTable
 
 $_idFieldName
 
 $_isPkAutoIncrement = true
 
 $_useIsObjectNew = false
 
 $_mainTableFields
 
 $_uniqueFields = null
 
 $_serializableFields = []
 
 $transactionManager
 
 $objectRelationProcessor
 
- Protected Attributes inherited from AbstractResource
 $serializer
 
 $_logger
 

Detailed Description

Rating resource model

@api

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m
Since
100.0.2

Definition at line 16 of file Rating.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Model\ResourceModel\Db\Context  $context,
\Psr\Log\LoggerInterface  $logger,
\Magento\Framework\Module\Manager  $moduleManager,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Review\Model\ResourceModel\Review\Summary  $reviewSummary,
  $connectionName = null 
)
Parameters
\Magento\Framework\Model\ResourceModel\Db\Context$context
\Psr\Log\LoggerInterface$logger
\Magento\Framework\Module\Manager$moduleManager
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Review\Model\ResourceModel\Review\Summary$reviewSummary
string$connectionName

Definition at line 45 of file Rating.php.

52  {
53  $this->moduleManager = $moduleManager;
54  $this->_storeManager = $storeManager;
55  $this->_logger = $logger;
56  $this->_reviewSummary = $reviewSummary;
57  parent::__construct($context, $connectionName);
58  }
$storeManager
$logger

Member Function Documentation

◆ _afterDelete()

_afterDelete ( \Magento\Framework\Model\AbstractModel  $object)
protected

Perform actions after object delete Prepare rating data for reaggregate all data for reviews

Parameters
\Magento\Framework\Model\AbstractModel$object
Returns
$this

Definition at line 277 of file Rating.php.

278  {
279  parent::_afterDelete($object);
280  if (!$this->moduleManager->isEnabled('Magento_Review')) {
281  return $this;
282  }
283  $data = $this->_getEntitySummaryData($object);
284  $summary = [];
285  foreach ($data as $row) {
286  $clone = clone $object;
287  $clone->addData($row);
288  $summary[$clone->getStoreId()][$clone->getEntityPkValue()] = $clone;
289  }
290  $this->_reviewSummary->reAggregate($summary);
291  return $this;
292  }

◆ _afterLoad()

_afterLoad ( \Magento\Framework\Model\AbstractModel  $object)
protected

Actions after load

Parameters
\Magento\Framework\Model\AbstractModel | \Magento\Review\Model\Rating$object
Returns
$this

Definition at line 113 of file Rating.php.

114  {
115  parent::_afterLoad($object);
116 
117  if (!$object->getId()) {
118  return $this;
119  }
120 
121  $connection = $this->getConnection();
122  $bind = [':rating_id' => (int)$object->getId()];
123  // load rating titles
124  $select = $connection->select()->from(
125  $this->getTable('rating_title'),
126  ['store_id', 'value']
127  )->where(
128  'rating_id=:rating_id'
129  );
130 
131  $result = $connection->fetchPairs($select, $bind);
132  if ($result) {
133  $object->setRatingCodes($result);
134  }
135 
136  // load rating available in stores
137  $object->setStores($this->getStores((int)$object->getId()));
138 
139  return $this;
140  }
$connection
Definition: bulk.php:13

◆ _afterSave()

_afterSave ( \Magento\Framework\Model\AbstractModel  $object)
protected

Actions after save

Parameters
\Magento\Framework\Model\AbstractModel | \Magento\Review\Model\Rating$object
Returns
$this

Definition at line 166 of file Rating.php.

167  {
168  parent::_afterSave($object);
169  if ($object->hasRatingCodes()) {
170  $this->processRatingCodes($object);
171  }
172 
173  if ($object->hasStores()) {
174  $this->processRatingStores($object);
175  }
176 
177  return $this;
178  }
processRatingStores(\Magento\Framework\Model\AbstractModel $object)
Definition: Rating.php:207
processRatingCodes(\Magento\Framework\Model\AbstractModel $object)
Definition: Rating.php:184

◆ _construct()

_construct ( )
protected

Resource initialization

Returns
void

Definition at line 65 of file Rating.php.

66  {
67  $this->_init('rating', 'rating_id');
68  }

◆ _getEntitySummaryData()

_getEntitySummaryData (   $object)
protected

Return data of rating summary

Parameters
\Magento\Review\Model\Rating$object
Returns
array

Definition at line 342 of file Rating.php.

343  {
344  $connection = $this->getConnection();
345 
346  $sumColumn = new \Zend_Db_Expr("SUM(rating_vote.{$connection->quoteIdentifier('percent')})");
347  $countColumn = new \Zend_Db_Expr("COUNT(*)");
348 
349  $select = $connection->select()->from(
350  ['rating_vote' => $this->getTable('rating_option_vote')],
351  ['entity_pk_value' => 'rating_vote.entity_pk_value', 'sum' => $sumColumn, 'count' => $countColumn]
352  )->join(
353  ['review' => $this->getTable('review')],
354  'rating_vote.review_id=review.review_id',
355  []
356  )->joinLeft(
357  ['review_store' => $this->getTable('review_store')],
358  'rating_vote.review_id=review_store.review_id',
359  ['review_store.store_id']
360  );
361  if (!$this->_storeManager->isSingleStoreMode()) {
362  $select->join(
363  ['rating_store' => $this->getTable('rating_store')],
364  'rating_store.rating_id = rating_vote.rating_id AND rating_store.store_id = review_store.store_id',
365  []
366  );
367  }
368  $select->join(
369  ['review_status' => $this->getTable('review_status')],
370  'review.status_id = review_status.status_id',
371  []
372  )->where(
373  'review_status.status_code = :status_code'
374  )->group(
375  'rating_vote.entity_pk_value'
376  )->group(
377  'review_store.store_id'
378  );
379  $bind = [':status_code' => self::RATING_STATUS_APPROVED];
380 
381  $entityPkValue = $object->getEntityPkValue();
382  if ($entityPkValue) {
383  $select->where('rating_vote.entity_pk_value = :pk_value');
384  $bind[':pk_value'] = $entityPkValue;
385  }
386 
387  return $connection->fetchAll($select, $bind);
388  }
$connection
Definition: bulk.php:13

◆ _getLoadSelect()

_getLoadSelect (   $field,
  $value,
  $object 
)
protected

Retrieve select object for load object data

Parameters
string$field
mixed$value
\Magento\Review\Model\Rating$object
Returns
\Magento\Framework\DB\Select

Definition at line 89 of file Rating.php.

90  {
91  $connection = $this->getConnection();
92 
93  $table = $this->getMainTable();
94  $storeId = (int)$this->_storeManager->getStore(\Magento\Store\Model\Store::ADMIN_CODE)->getId();
95  $select = parent::_getLoadSelect($field, $value, $object);
96  $codeExpr = $connection->getIfNullSql('title.value', "{$table}.rating_code");
97 
98  $select->joinLeft(
99  ['title' => $this->getTable('rating_title')],
100  $connection->quoteInto("{$table}.rating_id = title.rating_id AND title.store_id = ?", $storeId),
101  ['rating_code' => $codeExpr]
102  );
103 
104  return $select;
105  }
$value
Definition: gender.phtml:16
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14

◆ _initUniqueFields()

_initUniqueFields ( )
protected

Initialize unique fields

Returns
$this

Definition at line 75 of file Rating.php.

76  {
77  $this->_uniqueFields = [['field' => 'rating_code', 'title' => '']];
78  return $this;
79  }

◆ deleteAggregatedRatingsByProductId()

deleteAggregatedRatingsByProductId (   $productId)

Delete ratings by product id

Parameters
int$productId
Returns
$this

Definition at line 486 of file Rating.php.

487  {
488  $entityId = $this->getEntityIdByCode(\Magento\Review\Model\Rating::ENTITY_PRODUCT_CODE);
489  $connection = $this->getConnection();
490  $select = $connection->select()->from($this->getMainTable(), 'rating_id')->where('entity_id = :entity_id');
491  $ratingIds = $connection->fetchCol($select, [':entity_id' => $entityId]);
492 
493  if ($ratingIds) {
494  $where = ['entity_pk_value = ?' => (int)$productId, 'rating_id IN(?)' => $ratingIds];
495  $connection->delete($this->getTable('rating_option_vote_aggregated'), $where);
496  }
497 
498  return $this;
499  }
$connection
Definition: bulk.php:13

◆ deleteRatingData()

deleteRatingData (   $ratingId,
  $table,
array  $storeIds 
)
protected
Parameters
int$ratingId
string$table
array$storeIds
Returns
void

Definition at line 232 of file Rating.php.

233  {
234  if (empty($storeIds)) {
235  return;
236  }
237  $connection = $this->getConnection();
238  $connection->beginTransaction();
239  try {
240  $where = ['rating_id = ?' => $ratingId, 'store_id IN(?)' => $storeIds];
241  $connection->delete($table, $where);
242  $connection->commit();
243  } catch (\Exception $e) {
244  $this->_logger->critical($e);
245  $connection->rollBack();
246  }
247  }
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14

◆ getEntityIdByCode()

getEntityIdByCode (   $entityCode)

Get rating entity type id by code

Parameters
string$entityCode
Returns
int

Definition at line 468 of file Rating.php.

469  {
470  $select = $this->getConnection()->select()->from(
471  $this->getTable('rating_entity'),
472  ['entity_id']
473  )->where(
474  'entity_code = :entity_code'
475  );
476 
477  return $this->getConnection()->fetchOne($select, [':entity_code' => $entityCode]);
478  }

◆ getEntitySummary()

getEntitySummary (   $object,
  $onlyForCurrentStore = true 
)

Return array of rating summary

Parameters
\Magento\Review\Model\Rating$object
boolean$onlyForCurrentStore
Returns
array

Definition at line 301 of file Rating.php.

302  {
303  $data = $this->_getEntitySummaryData($object);
304 
305  if ($onlyForCurrentStore) {
306  foreach ($data as $row) {
307  if ($row['store_id'] == $this->_storeManager->getStore()->getId()) {
308  $object->addData($row);
309  }
310  }
311  return $object;
312  }
313 
314  $stores = $this->_storeManager->getStores();
315 
316  $result = [];
317  foreach ($data as $row) {
318  $clone = clone $object;
319  $clone->addData($row);
320  $result[$clone->getStoreId()] = $clone;
321  }
322 
323  $usedStoresId = array_keys($result);
324  foreach ($stores as $store) {
325  if (!in_array($store->getId(), $usedStoresId)) {
326  $clone = clone $object;
327  $clone->setCount(0);
328  $clone->setSum(0);
329  $clone->setStoreId($store->getId());
330  $result[$store->getId()] = $clone;
331  }
332  }
333  return array_values($result);
334  }

◆ getReviewSummary()

getReviewSummary (   $object,
  $onlyForCurrentStore = true 
)

Review summary

Parameters
\Magento\Review\Model\Rating$object
boolean$onlyForCurrentStore
Returns
array

Definition at line 397 of file Rating.php.

398  {
399  $connection = $this->getConnection();
400 
401  $sumColumn = new \Zend_Db_Expr("SUM(rating_vote.{$connection->quoteIdentifier('percent')})");
402  $countColumn = new \Zend_Db_Expr('COUNT(*)');
403  $select = $connection->select()->from(
404  ['rating_vote' => $this->getTable('rating_option_vote')],
405  ['sum' => $sumColumn, 'count' => $countColumn]
406  )->joinLeft(
407  ['review_store' => $this->getTable('review_store')],
408  'rating_vote.review_id = review_store.review_id',
409  ['review_store.store_id']
410  );
411  if (!$this->_storeManager->isSingleStoreMode()) {
412  $select->join(
413  ['rating_store' => $this->getTable('rating_store')],
414  'rating_store.rating_id = rating_vote.rating_id AND rating_store.store_id = review_store.store_id',
415  []
416  );
417  }
418  $select->where(
419  'rating_vote.review_id = :review_id'
420  )->group(
421  'rating_vote.review_id'
422  )->group(
423  'review_store.store_id'
424  );
425 
426  $data = $connection->fetchAll($select, [':review_id' => $object->getReviewId()]);
427 
428  if ($onlyForCurrentStore) {
429  foreach ($data as $row) {
430  if ($row['store_id'] == $this->_storeManager->getStore()->getId()) {
431  $object->addData($row);
432  }
433  }
434  return $object;
435  }
436 
437  $result = [];
438 
439  $stores = $this->_storeManager->getStore()->getResourceCollection()->load();
440 
441  foreach ($data as $row) {
442  $clone = clone $object;
443  $clone->addData($row);
444  $result[$clone->getStoreId()] = $clone;
445  }
446 
447  $usedStoresId = array_keys($result);
448 
449  foreach ($stores as $store) {
450  if (!in_array($store->getId(), $usedStoresId)) {
451  $clone = clone $object;
452  $clone->setCount(0);
453  $clone->setSum(0);
454  $clone->setStoreId($store->getId());
455  $result[$store->getId()] = $clone;
456  }
457  }
458 
459  return array_values($result);
460  }
$connection
Definition: bulk.php:13

◆ getStores()

getStores (   $ratingId)

Retrieve store IDs related to given rating

Parameters
int$ratingId
Returns
array

Definition at line 148 of file Rating.php.

149  {
150  $select = $this->getConnection()->select()->from(
151  $this->getTable('rating_store'),
152  'store_id'
153  )->where(
154  'rating_id = ?',
155  $ratingId
156  );
157  return $this->getConnection()->fetchCol($select);
158  }

◆ insertRatingData()

insertRatingData (   $table,
array  $data 
)
protected
Parameters
string$table
array$data
Returns
void

Definition at line 254 of file Rating.php.

255  {
256  if (empty($data)) {
257  return;
258  }
259  $connection = $this->getConnection();
260  $connection->beginTransaction();
261  try {
262  $connection->insertMultiple($table, $data);
263  $connection->commit();
264  } catch (\Exception $e) {
265  $this->_logger->critical($e);
266  $connection->rollBack();
267  }
268  }
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14

◆ processRatingCodes()

processRatingCodes ( \Magento\Framework\Model\AbstractModel  $object)
protected
Parameters
\Magento\Framework\Model\AbstractModel$object
Returns
$this

Definition at line 184 of file Rating.php.

185  {
186  $connection = $this->getConnection();
187  $ratingId = (int)$object->getId();
188  $table = $this->getTable('rating_title');
189  $select = $connection->select()->from($table, ['store_id', 'value'])
190  ->where('rating_id = :rating_id');
191  $old = $connection->fetchPairs($select, [':rating_id' => $ratingId]);
192  $new = array_filter(array_map('trim', $object->getRatingCodes()));
193  $this->deleteRatingData($ratingId, $table, array_keys(array_diff_assoc($old, $new)));
194 
195  $insert = [];
196  foreach (array_diff_assoc($new, $old) as $storeId => $title) {
197  $insert[] = ['rating_id' => $ratingId, 'store_id' => (int)$storeId, 'value' => $title];
198  }
199  $this->insertRatingData($table, $insert);
200  return $this;
201  }
$title
Definition: default.phtml:14
$old
Definition: website.php:27
insertRatingData($table, array $data)
Definition: Rating.php:254
deleteRatingData($ratingId, $table, array $storeIds)
Definition: Rating.php:232
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14

◆ processRatingStores()

processRatingStores ( \Magento\Framework\Model\AbstractModel  $object)
protected
Parameters
\Magento\Framework\Model\AbstractModel$object
Returns
$this

Definition at line 207 of file Rating.php.

208  {
209  $connection = $this->getConnection();
210  $ratingId = (int)$object->getId();
211  $table = $this->getTable('rating_store');
212  $select = $connection->select()->from($table, ['store_id'])
213  ->where('rating_id = :rating_id');
214  $old = $connection->fetchCol($select, [':rating_id' => $ratingId]);
215  $new = $object->getStores();
216  $this->deleteRatingData($ratingId, $table, array_diff($old, $new));
217 
218  $insert = [];
219  foreach (array_diff($new, $old) as $storeId) {
220  $insert[] = ['rating_id' => $ratingId, 'store_id' => (int)$storeId];
221  }
222  $this->insertRatingData($table, $insert);
223  return $this;
224  }
$old
Definition: website.php:27
insertRatingData($table, array $data)
Definition: Rating.php:254
deleteRatingData($ratingId, $table, array $storeIds)
Definition: Rating.php:232
$connection
Definition: bulk.php:13
$table
Definition: trigger.php:14

Field Documentation

◆ $_logger

$_logger
protected

Definition at line 35 of file Rating.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 25 of file Rating.php.

◆ $moduleManager

$moduleManager
protected

Definition at line 30 of file Rating.php.

◆ RATING_STATUS_APPROVED

const RATING_STATUS_APPROVED = 'Approved'

Definition at line 18 of file Rating.php.


The documentation for this class was generated from the following file: