Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: do a select in systemtag_object_mapping to see if tag exists alr…
…eady in db. if it does not exist alone insert the same or else do nothing

Signed-off-by: yemkareems <[email protected]>
  • Loading branch information
yemkareems authored and marcelklehr committed Jun 19, 2024
commit 8782b8724aa01ea7dbce91817b352613726cd133
26 changes: 20 additions & 6 deletions lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {

$this->assertTagsExist($tagIds);

$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();

$query = $this->connection->getQueryBuilder();
$query->insert(self::RELATION_TABLE)
->values([
Expand All @@ -126,12 +135,17 @@ public function assignTags(string $objId, string $objectType, $tagIds): void {

$tagsAssigned = [];
foreach ($tagIds as $tagId) {
try {
$query->setParameter('tagid', $tagId);
$query->execute();
$tagsAssigned[] = $tagId;
} catch (UniqueConstraintViolationException $e) {
// ignore existing relations
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
}
}

Expand Down