src/apis/StorageController.php
public function actionToggleFileTag()
    {
        $tagId = Yii::$app->request->getBodyParam('tagId');
        $fileId = Yii::$app->request->getBodyParam('fileId');

        $file = StorageFile::findOne($fileId);

        if (!$file) {
            throw new NotFoundHttpException("Unable to find the given file to toggle the tag.");
        }
    
        $relation = TagRelation::find()->where(['table_name' => TaggableTrait::cleanBaseTableName(StorageFile::tableName()), 'pk_id' => $fileId, 'tag_id' => $tagId])->one();

        if ($relation) {
            $relation->delete();

            return $file->tags;
        }

        $model = new TagRelation();
        $model->table_name = TaggableTrait::cleanBaseTableName(StorageFile::tableName());
        $model->pk_id = $fileId;
        $model->tag_id = $tagId;

        $model->save();
        return $file->tags;
    }
public function actionToggleFileTag()
    {
        $tagId = Yii::$app->request->getBodyParam('tagId');
        $fileId = Yii::$app->request->getBodyParam('fileId');

        $file = StorageFile::findOne($fileId);

        if (!$file) {
            throw new NotFoundHttpException("Unable to find the given file to toggle the tag.");
        }
    
        $relation = TagRelation::find()->where(['table_name' => TaggableTrait::cleanBaseTableName(StorageFile::tableName()), 'pk_id' => $fileId, 'tag_id' => $tagId])->one();

        if ($relation) {
            $relation->delete();

            return $file->tags;
        }

        $model = new TagRelation();
        $model->table_name = TaggableTrait::cleanBaseTableName(StorageFile::tableName());
        $model->pk_id = $fileId;
        $model->tag_id = $tagId;

        $model->save();
        return $file->tags;
    }
public function actionToggleFileTag ( ) { $ tagId = Yii :: $ app -> request -> getBodyParam ( 'tagId' ) ; $ fileId = Yii :: $ app -> request -> getBodyParam ( 'fileId' ) ; $ file = StorageFile :: findOne ( $ fileId ) ; if ( ! $ file ) { throw new NotFoundHttpException ( "Unable to find the given file to toggle the tag." ) ; } $ relation = TagRelation :: find ( ) -> where ( [ 'table_name' => TaggableTrait :: cleanBaseTableName ( StorageFile :: tableName ( ) ) , 'pk_id' => $ fileId , 'tag_id' => $ tagId ] ) -> one ( ) ; if ( $ relation ) { $ relation -> delete ( ) ; return $ file -> tags ; } $ model = new TagRelation ( ) ; $ model -> table_name = TaggableTrait :: cleanBaseTableName ( StorageFile :: tableName ( ) ) ; $ model -> pk_id = $ fileId ; $ model -> tag_id = $ tagId ; $ model -> save ( ) ; return $ file -> tags ; }
Toggle Tags for a given file.

If a relation exists, remove, otherwise add.

@return The array of associated tags for the given file.
@since 2.0.0
Toggle Tags for a given file .