@@ -133,6 +133,27 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
133133 }
134134
135135 $ this ->assertTagsExist ($ tagIds );
136+ $ this ->connection ->beginTransaction ();
137+
138+ $ query = $ this ->connection ->getQueryBuilder ();
139+ $ query ->select ('systemtagid ' )
140+ ->from (self ::RELATION_TABLE )
141+ ->where ($ query ->expr ()->in ('systemtagid ' , $ query ->createNamedParameter ($ tagIds , IQueryBuilder::PARAM_INT_ARRAY )))
142+ ->andWhere ($ query ->expr ()->eq ('objecttype ' , $ query ->createNamedParameter ($ objectType )))
143+ ->andWhere ($ query ->expr ()->eq ('objectid ' , $ query ->createNamedParameter ($ objId )));
144+ $ result = $ query ->executeQuery ();
145+ $ rows = $ result ->fetchAll ();
146+ $ existingTags = [];
147+ foreach ($ rows as $ row ) {
148+ $ existingTags [] = $ row ['systemtagid ' ];
149+ }
150+ //filter only tags that do not exist in db
151+ $ tagIds = array_diff ($ tagIds , $ existingTags );
152+ if (empty ($ tagIds )) {
153+ // no tags to insert so return here
154+ $ this ->connection ->commit ();
155+ return ;
156+ }
136157
137158 $ query = $ this ->connection ->getQueryBuilder ();
138159 $ query ->insert (self ::RELATION_TABLE )
@@ -153,6 +174,7 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {
153174 }
154175 }
155176
177+ $ this ->connection ->commit ();
156178 if (empty ($ tagsAssigned )) {
157179 return ;
158180 }
0 commit comments