Blame view

common/behaviors/RatingBehavior.php 1.24 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
      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',
2272ed1b   Yarik   Ratingbehavior fix.
24
25
                  ActiveRecord::EVENT_AFTER_DELETE => 'afterUpdate',
                  ActiveRecord::EVENT_AFTER_INSERT => 'afterUpdate',
17ca60dd   Yarik   Added comments to...
26
27
28
29
30
31
32
33
34
35
              ];
          }
          
          public function afterUpdate($event)
          {
              /**
               * @var Event        $event
               * @var CommentModel $owner
               */
              $owner = $this->owner;
fe7f596f   Yarik   Added comments to...
36
37
38
              if($owner->entity == Product::className() || $owner->entity == Articles::className()) {
                  $entity = $owner->entity;
                  $model = $entity::findOne($owner->entity_id);
17ca60dd   Yarik   Added comments to...
39
40
41
42
43
44
                  if($model != NULL) {
                      $model->recalculateRating();
                  }
              }
          }
      }