diff options
author | yemkareems <yemkareems@gmail.com> | 2024-05-30 15:38:06 +0530 |
---|---|---|
committer | Marcel Klehr <mklehr@gmx.net> | 2024-06-19 12:52:46 +0200 |
commit | d0883b5e85f757aafacfb6d67062f34299a25b34 (patch) | |
tree | c9021429acfe578ce652c9ff21abab207986b2b1 /lib | |
parent | 8782b8724aa01ea7dbce91817b352613726cd133 (diff) | |
download | nextcloud-server-d0883b5e85f757aafacfb6d67062f34299a25b34.tar.gz nextcloud-server-d0883b5e85f757aafacfb6d67062f34299a25b34.zip |
fix: filtering tags based on existing tags and doing a transaction around the select and insert
Signed-off-by: yemkareems <yemkareems@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/SystemTag/SystemTagObjectMapper.php | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/lib/private/SystemTag/SystemTagObjectMapper.php b/lib/private/SystemTag/SystemTagObjectMapper.php index 78e21d0cb27..852c3a28135 100644 --- a/lib/private/SystemTag/SystemTagObjectMapper.php +++ b/lib/private/SystemTag/SystemTagObjectMapper.php @@ -115,6 +115,7 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper { } $this->assertTagsExist($tagIds); + $this->connection->beginTransaction(); $query = $this->connection->getQueryBuilder(); $query->select('systemtagid') @@ -124,6 +125,12 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper { ->andWhere($query->expr()->eq('objectid', $query->createNamedParameter($objId))); $result = $query->executeQuery(); $rows = $result->fetchAll(); + $existingTags = []; + foreach ($rows as $k => $row) { + $existingTags[] = $row['systemtagid']; + } + //filter only tags that do not exist in db + $tagIds = array_diff($tagIds, $existingTags); $query = $this->connection->getQueryBuilder(); $query->insert(self::RELATION_TABLE) @@ -135,20 +142,16 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper { $tagsAssigned = []; foreach ($tagIds as $tagId) { - if(!in_array($tagId, array_column($rows, 'systemtagid'))) { - // tag not in db so create new one - try { - $query->setParameter('tagid', $tagId); - $query->execute(); - $tagsAssigned[] = $tagId; - } catch (UniqueConstraintViolationException $e) { - // ignore existing relations - } - } else { - //tag exists already don't insert + try { + $query->setParameter('tagid', $tagId); + $query->execute(); + $tagsAssigned[] = $tagId; + } catch (UniqueConstraintViolationException $e) { + // ignore existing relations } } + $this->connection->commit(); if (empty($tagsAssigned)) { return; } |