Example #1
0
 public static function afterTopicDelete($topicId)
 {
     $tagTopics = static::find()->select(['tag_id'])->where(['topic_id' => $topicId])->asArray()->all();
     if (empty($tagTopics)) {
         return;
     }
     $tagIds = ArrayHelper::getColumn($tagTopics, 'tag_id');
     Tag::updateAllCounters(['topic_count' => -1], ['and', ['id' => $tagIds], ['>', 'topic_count', 0]]);
     static::deleteAll(['topic_id' => $topicId]);
 }
Example #2
0
 public function afterSave($insert, $changedAttributes)
 {
     if (!$insert) {
         Yii::$app->db->createCommand()->delete('author_book', ['bookId' => $this->id])->execute();
         Yii::$app->db->createCommand()->delete('book_format', ['bookId' => $this->id])->execute();
         Yii::$app->db->createCommand()->delete('book_tag', ['bookId' => $this->id])->execute();
     }
     if (!empty($this->authors)) {
         $authorInsert = [];
         foreach ($this->authors as $author) {
             array_push($authorInsert, [$author, $this->id]);
         }
         Yii::$app->db->createCommand()->batchInsert('author_book', ['authorId', 'bookId'], $authorInsert)->execute();
     }
     if (!empty($this->formats)) {
         $formatInsert = [];
         foreach ($this->formats as $format) {
             array_push($formatInsert, [$format, $this->id]);
         }
         Yii::$app->db->createCommand()->batchInsert('book_format', ['formatId', 'bookId'], $formatInsert)->execute();
     }
     if (!empty($this->tags)) {
         $tagInsert = [];
         foreach ($this->tags as $tag) {
             array_push($tagInsert, [$tag, $this->id]);
         }
         Yii::$app->db->createCommand()->batchInsert('book_tag', ['tagId', 'bookId'], $tagInsert)->execute();
         Tag::updateAllCounters(['frequency' => 1], ['id' => array_map('intval', $this->tags)]);
     }
     parent::afterSave($insert, $changedAttributes);
 }