Blame view

common/behaviors/RatingBehavior.php 1.11 KB
4253cbec   root   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  <?php
      
      namespace common\behaviors;
      
      use common\models\Articles;
      use common\modules\comment\models\CommentModel;
      use common\modules\product\models\Product;
      use yii\base\Behavior;
      use yii\base\Event;
      use yii\db\ActiveRecord;
      
      /**
       * Class RatingBehavior
       * @property CommentModel $owner
       * @package common\behaviors
       */
      class RatingBehavior extends Behavior
      {
          
          public function events()
          {
              return [
                  ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate',
              ];
          }
          
          public function afterUpdate($event)
          {
              /**
               * @var Event        $event
               * @var CommentModel $owner
               */
              $owner = $this->owner;
              if($owner->entity == Product::className() || $owner->entity == Articles::className()) {
                  $entity = $owner->entity;
                  $model = $entity::findOne($owner->entity_id);
                  if($model != NULL) {
                      $model->recalculateRating();
                  }
              }
          }
      }