/** * @inheritdoc */ public function afterDelete() { Comment::deleteAll(['commentable_type' => Comment::COMMENTABLE_PHOTO, 'commentable_id' => $this->id]); CommentCount::deleteAll(['commentable_type' => CommentCount::COMMENTABLE_PHOTO, 'commentable_id' => $this->id]); }
/** * Get amount of photos in album * @return int */ public function getCommentsCount() { return CommentCount::getCommentCount($this->id, CommentCount::COMMENTABLE_MATCH); }
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); }