public function afterSave($insert, $changedAttributes) { switch ($this->commentable_type) { case self::COMMENTABLE_POST: case self::COMMENTABLE_TRANSFER: case self::COMMENTABLE_MATCH: case self::COMMENTABLE_VIDEO: case self::COMMENTABLE_INQUIRER: $commentCount = CommentCount::find()->where(['commentable_id' => $this->commentable_id, 'commentable_type' => $this->commentable_type])->one(); if (empty($commentCount->id)) { $commentCount = new CommentCount(); $commentCount->commentable_id = $this->commentable_id; $commentCount->commentable_type = $this->commentable_type; } $count = Comment::find()->where(['commentable_type' => $this->commentable_type, 'commentable_id' => $this->commentable_id])->count(); $commentCount->count = $count; $commentCount->save(false); break; case self::COMMENTABLE_PHOTO: case self::COMMENTABLE_ALBUM: if ($this->commentable_type == self::COMMENTABLE_ALBUM) { $albumID = $this->commentable_id; } else { $albumTable = Album::tableName(); $assetTable = Asset::tableName(); $album = (new \yii\db\Query())->select("{$albumTable}.id")->from($albumTable)->innerJoin($assetTable, "{$assetTable}.assetable_id = {$albumTable}.id")->where(["{$assetTable}.id" => $this->commentable_id, "{$assetTable}.assetable_type" => Asset::ASSETABLE_ALBUM])->one(); if (isset($album['id'])) { $albumID = $album['id']; } } if (isset($albumID)) { $commentCount = CommentCount::find()->where(['commentable_id' => $albumID, 'commentable_type' => self::COMMENTABLE_ALBUM])->one(); if (empty($commentCount->id)) { $commentCount = new CommentCount(); $commentCount->commentable_id = $albumID; $commentCount->commentable_type = self::COMMENTABLE_ALBUM; $commentCount->count = 0; } // $commentCount->count++; $albumCount = Comment::find()->where(['commentable_type' => $this->commentable_type, 'commentable_id' => $this->commentable_id])->count(); $assetTable = Asset::tableName(); $albumTable = self::tableName(); $ids = (new \yii\db\Query())->select("{$assetTable}.id")->from($assetTable)->innerJoin($albumTable, "{$albumTable}.id = {$assetTable}.assetable_id")->where(["{$albumTable}.id" => $albumID, "{$assetTable}.assetable_type" => Asset::ASSETABLE_ALBUM])->all(); $assetIDs = []; foreach ($ids as $id) { $assetIDs[] = (int) $id['id']; } $assetsCount = Comment::find()->where(['commentable_type' => self::COMMENTABLE_PHOTO, 'commentable_id' => $assetIDs])->count(); $commentCount->count = $albumCount + $assetsCount; $commentCount->save(false); } break; default: break; } parent::afterSave($insert, $changedAttributes); }
/** * Get block with video reviews * @return array Data */ public static function getVideoNews() { $postTable = Post::tableName(); $assetTable = Asset::tableName(); // Video review $videoReviewNews = VideoPost::find()->where(['is_public' => 1])->orderBy(['created_at' => SORT_DESC])->limit(3)->all(); if (count($videoReviewNews) == 0) { return false; } $block = ['view' => '@frontend/views/blocks/review_news_block', 'data' => compact('videoReviewNews')]; return $block; }