I tried to get a related entity when it does not exist yet, and I got this error:

Notice: Trying to get property of non-object in relation_get_related_entity() (line 764 of /something/sites/all/modules/relation/relation.module)

and several other errors after that.

I tried looking for a way to verify the existence of the relation before actually getting it but relation_relation_exists() actually asks you to provide all the related entities, which I cannot provide because I want to create the other related entity on-the-fly.

My solution right now is to modify relation_get_related_entity() so that it returns gracefully when relations are not found, instead of throwing warnings & errors & WSOD:

diff --git a/src/sites/all/modules/relation/relation.module b/src/sites/all/modules/relation/relation.module
--- a/src/sites/all/modules/relation/relation.module
+++ b/src/sites/all/modules/relation/relation.module
@@ -760,6 +760,9 @@
   }
   $results = $query->execute();
   $result = reset($results);
+  if (empty($result)) {
+    return FALSE;
+  }
   $relation = relation_load($result->rid);
   $request_key = $entity_type . ':' . $entity_id;
   $entities = $relation->endpoints[LANGUAGE_NONE];

I'm not exactly sure that it is the intended behavior, but shouldn't this make the function more versatile and actually usable?

Comments

naught101’s picture

Status: Active » Fixed

Thanks, fixed in 7.x-1.x (commit 3fc1937)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.