Blame view

common/behaviors/RatingBehavior.php 1.11 KB
17ca60dd   Yarik   Added comments to...
1
2
3
4
  <?php
      
      namespace common\behaviors;
      
fe7f596f   Yarik   Added comments to...
5
      use common\models\Articles;
17ca60dd   Yarik   Added comments to...
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
      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;
fe7f596f   Yarik   Added comments to...
34
35
36
              if($owner->entity == Product::className() || $owner->entity == Articles::className()) {
                  $entity = $owner->entity;
                  $model = $entity::findOne($owner->entity_id);
17ca60dd   Yarik   Added comments to...
37
38
39
40
41
42
                  if($model != NULL) {
                      $model->recalculateRating();
                  }
              }
          }
      }