Browse Source

added workaround for Upsert issues with DB Api using postgres

logicp 7 years ago
parent
commit
6025e844a1
1 changed files with 25 additions and 9 deletions
  1. 25 9
      src/Entity/Heartbeat.php

+ 25 - 9
src/Entity/Heartbeat.php

@@ -631,15 +631,31 @@ class Heartbeat extends RevisionableContentEntityBase implements HeartbeatInterf
    * @return \Drupal\Core\Database\StatementInterface|int|null
    */
   public static function updateFriendship($uid, $uid_target, $unixtime, $friendStatus) {
-    $query = Database::getConnection()->upsert('heartbeat_friendship')
-      ->fields(array(
-        'uid' => $uid,
-        'uid_target' => $uid_target,
-        'created' => $unixtime,
-        'status' => $friendStatus,
-      ))
-      ->key('uid_relation');
-    return $query->execute();
+//    $query = Database::getConnection()->upsert('heartbeat_friendship')
+//      ->fields(array(
+//        'uid' => $uid,
+//        'uid_target' => $uid_target,
+//        'created' => $unixtime,
+//        'status' => $friendStatus,
+//      ))
+//      ->key('uid_relation');
+//    return $query->execute();
+    $update = Database::getConnection()->update('heartbeat_friendship')
+      ->fields(['status' => $friendStatus])
+      ->condition('uid', $uid, '=')
+      ->condition('uid_target', $uid_target, '=');
+    if (!$update->execute()) {
+      $insert = Database::getConnection()->insert('heartbeat_friendship')
+        ->fields([
+          'uid' => $uid,
+          'uid_target' => $uid_target,
+          'created' => $unixtime,
+          'status' => $friendStatus
+        ]);
+      if (!$insert->execute()) {
+        \Drupal::logger('Heartbeat')->error('Unable to update friendship between %uid and %uid_target', array('%uid' => $uid, '%uid_target' => $uid_target));
+      }
+    }
   }