Example #1
0
 /**
  * Updates tag words.
  * @throws Exception
  */
 protected function _updateWords()
 {
     try {
         $vocabulary = [];
         $allWords = $this->_prepareWords();
         $this->_addNewWords($allWords);
         $query = (new Query())->from(Vocabulary::tableName())->where(['word' => $allWords]);
         foreach ($query->each() as $vocabularyNew) {
             $vocabulary[$vocabularyNew['id']] = [$vocabularyNew['id'], $this->id];
         }
         if (!empty($vocabulary)) {
             Yii::$app->db->createCommand()->batchInsert('{{%podium_vocabulary_junction}}', ['word_id', 'post_id'], array_values($vocabulary))->execute();
         }
         $query = (new Query())->from('{{%podium_vocabulary_junction}}')->where(['post_id' => $this->id]);
         foreach ($query->each() as $junk) {
             if (!array_key_exists($junk['word_id'], $vocabulary)) {
                 Yii::$app->db->createCommand()->delete('{{%podium_vocabulary_junction}}', ['id' => $junk['id']])->execute();
             }
         }
     } catch (Exception $e) {
         Log::error($e->getMessage(), null, __METHOD__);
         throw $e;
     }
 }