Commit b409d90948fa1cdc887b37fed2da4cd2b4e6aaf0

Authored by Yarik
1 parent 30415cab

Rating caching

@@ -34,7 +34,7 @@ @@ -34,7 +34,7 @@
34 * @var string comment model class 34 * @var string comment model class
35 */ 35 */
36 public $commentModelClass = null; 36 public $commentModelClass = null;
37 - 37 +
38 public $ratingModelClass = null; 38 public $ratingModelClass = null;
39 39
40 /** 40 /**
@@ -63,7 +63,7 @@ @@ -63,7 +63,7 @@
63 * @var bool 63 * @var bool
64 */ 64 */
65 public $enableRating = true; 65 public $enableRating = true;
66 - 66 +
67 /** 67 /**
68 * Whether to enable comment premoderate or not. 68 * Whether to enable comment premoderate or not.
69 * 69 *
@@ -72,6 +72,11 @@ @@ -72,6 +72,11 @@
72 public $enablePremoderate = true; 72 public $enablePremoderate = true;
73 73
74 /** 74 /**
  75 + * @var string[] $cacheModels
  76 + */
  77 + public $cacheModels = [];
  78 +
  79 + /**
75 * Initializes the module. 80 * Initializes the module.
76 * This method is called after the module is created and initialized with property values 81 * This method is called after the module is created and initialized with property values
77 * given in configuration. The default implementation will initialize 82 * given in configuration. The default implementation will initialize
@@ -99,7 +104,7 @@ @@ -99,7 +104,7 @@
99 } 104 }
100 parent::init(); 105 parent::init();
101 } 106 }
102 - 107 +
103 /** 108 /**
104 * Prompt to register current module 109 * Prompt to register current module
105 * 110 *
@@ -110,4 +115,33 @@ @@ -110,4 +115,33 @@
110 throw new InvalidConfigException(\Yii::t('artbox-comment', 'Register artbox-comment module')); 115 throw new InvalidConfigException(\Yii::t('artbox-comment', 'Register artbox-comment module'));
111 } 116 }
112 117
  118 + /**
  119 + * Rating model factory
  120 + *
  121 + * @param $config
  122 + *
  123 + * @return \yii\db\ActiveRecord
  124 + */
  125 + public function createRating($config)
  126 + {
  127 + /**
  128 + * @var \yii\db\ActiveRecord $model
  129 + */
  130 + $model = \Yii::createObject(
  131 + array_merge(
  132 + [
  133 + 'class' => $this->ratingModelClass,
  134 + ],
  135 + $config
  136 + )
  137 + );
  138 + $behaviors = [];
  139 + foreach ($this->cacheModels as $cacheModel) {
  140 + $behaviors[] = \Yii::createObject($cacheModel);
  141 + }
  142 + if (!empty($behaviors)) {
  143 + $model->attachBehaviors($behaviors);
  144 + }
  145 + return $model;
  146 + }
113 } 147 }
controllers/DefaultController.php
@@ -3,7 +3,6 @@ @@ -3,7 +3,6 @@
3 namespace artbox\webcomment\controllers; 3 namespace artbox\webcomment\controllers;
4 4
5 use artbox\webcomment\models\CommentModel; 5 use artbox\webcomment\models\CommentModel;
6 - use artbox\webcomment\models\RatingModel;  
7 use artbox\webcomment\Module; 6 use artbox\webcomment\Module;
8 use yii\filters\AccessControl; 7 use yii\filters\AccessControl;
9 use yii\filters\VerbFilter; 8 use yii\filters\VerbFilter;
@@ -45,7 +44,7 @@ @@ -45,7 +44,7 @@
45 ], 44 ],
46 ]; 45 ];
47 } 46 }
48 - 47 +
49 /** 48 /**
50 * Create comment. 49 * Create comment.
51 * 50 *
@@ -80,11 +79,7 @@ @@ -80,11 +79,7 @@
80 $model->setAttributes($entity_data); 79 $model->setAttributes($entity_data);
81 if ($model->save()) { 80 if ($model->save()) {
82 if (empty($model->parent_id) && $module->enableRating) { 81 if (empty($model->parent_id) && $module->enableRating) {
83 - $ratingModelClass = $module->ratingModelClass;  
84 - /**  
85 - * @var RatingModel $rating  
86 - */  
87 - $rating = new $ratingModelClass( 82 + $rating = $module->createRating(
88 [ 83 [
89 'model' => $model::className(), 84 'model' => $model::className(),
90 'model_id' => $model->primaryKey, 85 'model_id' => $model->primaryKey,
models/CommentModel.php
@@ -33,6 +33,7 @@ @@ -33,6 +33,7 @@
33 * @property string $link 33 * @property string $link
34 * @property \artbox\webcomment\models\CommentModel[] $children 34 * @property \artbox\webcomment\models\CommentModel[] $children
35 * @property \artbox\order\models\Customer $customer 35 * @property \artbox\order\models\Customer $customer
  36 + * @property \artbox\webcomment\models\RatingModel $rating
36 */ 37 */
37 class CommentModel extends ActiveRecord implements CommentInterface 38 class CommentModel extends ActiveRecord implements CommentInterface
38 { 39 {
@@ -49,7 +50,7 @@ @@ -49,7 +50,7 @@
49 public $encryptedEntity; 50 public $encryptedEntity;
50 51
51 public $entityId; 52 public $entityId;
52 - 53 +
53 /** 54 /**
54 * @inheritdoc 55 * @inheritdoc
55 */ 56 */
@@ -85,7 +86,7 @@ @@ -85,7 +86,7 @@
85 ]; 86 ];
86 return $scenarios; 87 return $scenarios;
87 } 88 }
88 - 89 +
89 /** 90 /**
90 * @inheritdoc 91 * @inheritdoc
91 */ 92 */
@@ -93,7 +94,7 @@ @@ -93,7 +94,7 @@
93 { 94 {
94 return '{{%artbox_comment}}'; 95 return '{{%artbox_comment}}';
95 } 96 }
96 - 97 +
97 /** 98 /**
98 * @inheritdoc 99 * @inheritdoc
99 */ 100 */
@@ -139,7 +140,7 @@ @@ -139,7 +140,7 @@
139 ], 140 ],
140 ]; 141 ];
141 } 142 }
142 - 143 +
143 /** 144 /**
144 * @inheritdoc 145 * @inheritdoc
145 */ 146 */
@@ -188,7 +189,7 @@ @@ -188,7 +189,7 @@
188 ], 189 ],
189 ]; 190 ];
190 } 191 }
191 - 192 +
192 /** 193 /**
193 * @inheritdoc 194 * @inheritdoc
194 */ 195 */
@@ -212,7 +213,7 @@ @@ -212,7 +213,7 @@
212 'entity_id' => \Yii::t('artbox-comment', 'Entity ID'), 213 'entity_id' => \Yii::t('artbox-comment', 'Entity ID'),
213 ]; 214 ];
214 } 215 }
215 - 216 +
216 /** 217 /**
217 * Set Entity of Comment model 218 * Set Entity of Comment model
218 * 219 *
@@ -222,7 +223,7 @@ @@ -222,7 +223,7 @@
222 { 223 {
223 $this->entity = $entity; 224 $this->entity = $entity;
224 } 225 }
225 - 226 +
226 /** 227 /**
227 * Get Entity of Comment model 228 * Get Entity of Comment model
228 * 229 *
@@ -232,7 +233,7 @@ @@ -232,7 +233,7 @@
232 { 233 {
233 return $this->entity; 234 return $this->entity;
234 } 235 }
235 - 236 +
236 /** 237 /**
237 * Get ActiveDataProvider of comments for particular Entity according to its EntityId 238 * Get ActiveDataProvider of comments for particular Entity according to its EntityId
238 * 239 *
@@ -281,7 +282,7 @@ @@ -281,7 +282,7 @@
281 ] 282 ]
282 ); 283 );
283 } 284 }
284 - 285 +
285 /** 286 /**
286 * Delete comment 287 * Delete comment
287 * 288 *
@@ -296,7 +297,7 @@ @@ -296,7 +297,7 @@
296 } 297 }
297 return false; 298 return false;
298 } 299 }
299 - 300 +
300 /** 301 /**
301 * Set EntityId of Comment model 302 * Set EntityId of Comment model
302 * 303 *
@@ -306,7 +307,7 @@ @@ -306,7 +307,7 @@
306 { 307 {
307 $this->entityId = $entityId; 308 $this->entityId = $entityId;
308 } 309 }
309 - 310 +
310 /** 311 /**
311 * Get EntityId of Comment model 312 * Get EntityId of Comment model
312 * 313 *
@@ -316,7 +317,7 @@ @@ -316,7 +317,7 @@
316 { 317 {
317 return $this->entityId; 318 return $this->entityId;
318 } 319 }
319 - 320 +
320 /** 321 /**
321 * Get children relation for current comment 322 * Get children relation for current comment
322 * 323 *
@@ -328,7 +329,7 @@ @@ -328,7 +329,7 @@
328 ->andFilterWhere([ 'status' => self::STATUS_ACTIVE ]) 329 ->andFilterWhere([ 'status' => self::STATUS_ACTIVE ])
329 ->inverseOf('parent'); 330 ->inverseOf('parent');
330 } 331 }
331 - 332 +
332 /** 333 /**
333 * Get parent relation for current comment 334 * Get parent relation for current comment
334 * 335 *
@@ -339,7 +340,7 @@ @@ -339,7 +340,7 @@
339 return $this->hasOne(self::className(), [ 'id' => 'parent_id' ]) 340 return $this->hasOne(self::className(), [ 'id' => 'parent_id' ])
340 ->inverseOf('children'); 341 ->inverseOf('children');
341 } 342 }
342 - 343 +
343 /** 344 /**
344 * Get customer relation for current comment 345 * Get customer relation for current comment
345 * 346 *
@@ -349,7 +350,7 @@ @@ -349,7 +350,7 @@
349 { 350 {
350 return $this->hasOne(self::getCustomerClass(), [ 'id' => 'customer_id' ]); 351 return $this->hasOne(self::getCustomerClass(), [ 'id' => 'customer_id' ]);
351 } 352 }
352 - 353 +
353 /** 354 /**
354 * Get rating relation for current model 355 * Get rating relation for current model
355 * 356 *
@@ -366,7 +367,7 @@ @@ -366,7 +367,7 @@
366 ] 367 ]
367 ); 368 );
368 } 369 }
369 - 370 +
370 /** 371 /**
371 * Get entity model for current comment or false if not ActiveRecord 372 * Get entity model for current comment or false if not ActiveRecord
372 * 373 *
@@ -387,7 +388,7 @@ @@ -387,7 +388,7 @@
387 return false; 388 return false;
388 } 389 }
389 } 390 }
390 - 391 +
391 /** 392 /**
392 * Get Customer model name 393 * Get Customer model name
393 * 394 *