mirror of
https://github.com/nextcloud/server.git
synced 2026-02-27 03:50:37 -05:00
Merge pull request #45995 from nextcloud/backport/45580/stable29
[stable29] fix: avoid duplicate tag inserts by checking if the mapping exists already in db
This commit is contained in:
commit
a0dc778e14
1 changed files with 22 additions and 0 deletions
|
|
@ -133,6 +133,27 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper {
|
|||
}
|
||||
|
||||
$this->assertTagsExist($tagIds);
|
||||
$this->connection->beginTransaction();
|
||||
|
||||
$query = $this->connection->getQueryBuilder();
|
||||
$query->select('systemtagid')
|
||||
->from(self::RELATION_TABLE)
|
||||
->where($query->expr()->in('systemtagid', $query->createNamedParameter($tagIds, IQueryBuilder::PARAM_INT_ARRAY)))
|
||||
->andWhere($query->expr()->eq('objecttype', $query->createNamedParameter($objectType)))
|
||||
->andWhere($query->expr()->eq('objectid', $query->createNamedParameter($objId)));
|
||||
$result = $query->executeQuery();
|
||||
$rows = $result->fetchAll();
|
||||
$existingTags = [];
|
||||
foreach ($rows as $row) {
|
||||
$existingTags[] = $row['systemtagid'];
|
||||
}
|
||||
//filter only tags that do not exist in db
|
||||
$tagIds = array_diff($tagIds, $existingTags);
|
||||
if (empty($tagIds)) {
|
||||
// no tags to insert so return here
|
||||
$this->connection->commit();
|
||||
return;
|
||||
}
|
||||
|
||||
$query = $this->connection->getQueryBuilder();
|
||||
$query->insert(self::RELATION_TABLE)
|
||||
|
|
@ -153,6 +174,7 @@ class SystemTagObjectMapper implements ISystemTagObjectMapper {
|
|||
}
|
||||
}
|
||||
|
||||
$this->connection->commit();
|
||||
if (empty($tagsAssigned)) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue