Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

General improvements to RatableBehavior::saveRating() and RatableBeha…

…vior::removeRating()

By caching the $oldRating from RatableBehavior::isRatedBy(), we can remove an extra query max in both cases, improving performance.

Also removed cascading deletes and callbacks from RatableBehavior::saveRating()
  • Loading branch information...
commit b828cc942468f7fbfd19816d5e443b2e52484d94 1 parent 60bad8a
@josegonzalez josegonzalez authored
Showing with 36 additions and 19 deletions.
  1. +36 −19 Model/Behavior/RatableBehavior.php
View
55 Model/Behavior/RatableBehavior.php
@@ -117,25 +117,36 @@ public function setup(Model $Model, $settings = array()) {
public function saveRating(Model $Model, $foreignKey = null, $userId = null, $value = 0) {
$type = 'saveRating';
$this->beforeRateCallback($Model, compact('foreignKey', 'userId', 'value', 'update', 'type'));
- if (!$this->isRatedBy($Model, $foreignKey, $userId) || $this->settings[$Model->alias]['update'] == true) {
+ $oldRating = $this->isRatedBy($Model, $foreignKey, $userId);
+ if (!$oldRating || $this->settings[$Model->alias]['update'] == true) {
$data['Rating']['foreign_key'] = $foreignKey;
$data['Rating']['model'] = $Model->alias;
$data['Rating']['user_id'] = $userId;
$data['Rating']['value'] = $value;
- $oldRating = null;
- $update = false;
if ($this->settings[$Model->alias]['update'] == true) {
$update = true;
- $oldRating = $this->oldRating = $Model->Rating->find('first', array(
- 'recursive' => -1,
- 'conditions' => array(
+ $this->oldRating = $oldRating;
+ if (!empty($oldRating)) {
+ if (is_array($foreignKey)) {
+ $oldRating = $this->oldRating = $Model->Rating->find('first', array(
+ 'recursive' => -1,
+ 'conditions' => array(
+ 'Rating.model' => $Model->alias,
+ 'Rating.foreign_key' => $foreignKey,
+ 'Rating.user_id' => $userId
+ )
+ ));
+ }
+
+ $Model->Rating->deleteAll(array(
'Rating.model' => $Model->alias,
'Rating.foreign_key' => $foreignKey,
- 'Rating.user_id' => $userId)));
- $Model->Rating->deleteAll(array(
- 'Rating.model' => $Model->alias,
- 'Rating.foreign_key' => $foreignKey,
- 'Rating.user_id' => $userId));
+ 'Rating.user_id' => $userId
+ ), false, false);
+ }
+ } else {
+ $oldRating = null;
+ $update = false;
}
$Model->Rating->create();
@@ -167,18 +178,24 @@ public function saveRating(Model $Model, $foreignKey = null, $userId = null, $va
public function removeRating(Model $Model, $foreignKey = null, $userId = null) {
$type = 'removeRating';
$this->beforeRateCallback($Model, compact('foreignKey', 'userId', 'update', 'type'));
- if ($this->isRatedBy($Model, $foreignKey, $userId)) {
+ $oldRating = $this->isRatedBy($Model, $foreignKey, $userId);
+ if ($oldRating) {
$data['Rating']['foreign_key'] = $foreignKey;
$data['Rating']['model'] = $Model->alias;
$data['Rating']['user_id'] = $userId;
- $oldRating = null;
$update = true;
- $oldRating = $this->oldRating = $Model->Rating->find('first', array(
- 'recursive' => -1,
- 'conditions' => array(
- 'Rating.model' => $Model->alias,
- 'Rating.foreign_key' => $foreignKey,
- 'Rating.user_id' => $userId)));
+ $this->oldRating = $oldRating;
+ if (is_array($foreignKey)) {
+ $oldRating = $this->oldRating = $Model->Rating->find('first', array(
+ 'recursive' => -1,
+ 'conditions' => array(
+ 'Rating.model' => $Model->alias,
+ 'Rating.foreign_key' => $foreignKey,
+ 'Rating.user_id' => $userId
+ )
+ ));
+ }
+
$Model->Rating->deleteAll(array(
'Rating.model' => $Model->alias,
'Rating.foreign_key' => $foreignKey,
Please sign in to comment.
Something went wrong with that request. Please try again.