/** * @inheritdoc */ public function afterSave($insert, $changedAttributes) { TagPost::deleteAll(['post_id' => $this->id]); if (is_array($this->tags) && !empty($this->tags)) { $values = []; foreach ($this->tags as $id) { $values[] = [$this->id, $id]; } self::getDb()->createCommand()->batchInsert(TagPost::tableName(), ['post_id', 'tag_id'], $values)->execute(); } parent::afterSave($insert, $changedAttributes); }
/** * return post that relatives to tag * @return ActiveQuery */ public function getTagPosts() { return $this->hasMany(TagPost::className(), ['tag_id' => 'id']); }