@@ -60,7 +60,7 @@ public function findForGroupForFile(int $fileId, string $groupName): FileMetadat
6060 $ qb = $ this ->db ->getQueryBuilder ();
6161 $ qb ->select ('* ' )
6262 ->from ($ this ->getTableName ())
63- ->where ($ qb ->expr ()->eq ('id ' , $ qb ->createNamedParameter ($ fileId , IQueryBuilder::PARAM_INT_ARRAY )))
63+ ->where ($ qb ->expr ()->eq ('id ' , $ qb ->createNamedParameter ($ fileId , IQueryBuilder::PARAM_INT )))
6464 ->andWhere ($ qb ->expr ()->eq ('group_name ' , $ qb ->createNamedParameter ($ groupName , IQueryBuilder::PARAM_STR )));
6565
6666 return $ this ->findEntity ($ qb );
@@ -111,7 +111,7 @@ public function clear(int $fileId): void {
111111 /**
112112 * Updates an entry in the db from an entity
113113 *
114- * @param Entity $entity the entity that should be created
114+ * @param FileMetadata $entity the entity that should be created
115115 * @return FileMetadata the saved entity with the set id
116116 * @throws Exception
117117 * @throws \InvalidArgumentException if entity has no id
@@ -148,4 +148,30 @@ public function update(Entity $entity): FileMetadata {
148148
149149 return $ entity ;
150150 }
151+
152+ /**
153+ * Override the insertOrUpdate as we could be in a transaction in which case we can not afford on error.
154+ *
155+ * @param FileMetadata $entity the entity that should be created/updated
156+ * @return FileMetadata the saved entity with the (new) id
157+ * @throws Exception
158+ * @throws \InvalidArgumentException if entity has no id
159+ */
160+ public function insertOrUpdate (Entity $ entity ): FileMetadata {
161+ try {
162+ $ existingEntity = $ this ->findForGroupForFile ($ entity ->getId (), $ entity ->getGroupName ());
163+ } catch (\Throwable ) {
164+ $ existingEntity = null ;
165+ }
166+
167+ if ($ existingEntity !== null ) {
168+ if ($ entity ->getValue () !== $ existingEntity ->getValue ()) {
169+ return $ this ->update ($ entity );
170+ } else {
171+ return $ existingEntity ;
172+ }
173+ } else {
174+ return parent ::insertOrUpdate ($ entity );
175+ }
176+ }
151177}
0 commit comments