Having written a fair bit of code using the User Relationships API, I recommend making the following changes to the API:

  1. The array returned by user_relationships_relationship_types_load() should be indexed by rtid, instead of just an arbitrary index. That way, if you happen to have a relationship object, you can easily gather additional information about it's type.
  2. A new function, user_relationships_load_relationships($criteria), should be added. This function would behave like the recently added user_relationships_count_relationships($criteria) function, except that instead of returning the count of relationships that match the criteria, it returns an array of relationship objects that match. [See http://drupal.org/node/183011 .] If 'uid' is specified in the criteria, the array would be indexed by $relatee, where $relatee is relative to the uid specified in the criteria; otherwise, it would have an arbitrary (numeric) index. That way, you could do the following:
    foreach (user_relationships_load_relationships(array ('uid' => $user_id)) as $relatee_id => $relationship) {
        $relatee = user_load(array('uid' => $relatee_id));
        ...
    }
    

    This would save folks from having to do $relatee_id = ($relationship->requester_id == $user_id) ? $relationship->requestee_id : $relationship->requester_id; virtually everywhere they load relationships.

  3. The user_relationships_load_all_for_user($uid) could be deprecated, as it's equivalent to user_relationships_load_relationships(array('uid' => $value)).
  4. The user_relationships_relationship_load($rid) could also be deprecated, as it's equivalent to user_relationships_load_relationships(array('rid' => $value)).

What do folks think? Does this sound reasonable?

CommentFileSizeAuthor
#3 urapi.patch4.47 KBprfctns6@gmail.com
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

prfctns6@gmail.com’s picture

I really want user_relationships_load_relationships($criteria) to return an array indexed by relatee whenever possible. But it seems a little weird to have that behavior only happen if $criteria contains a key of 'uid' — it seems like the behavior is hidden from the API user. Another option is to have it accept an optional second parameter, $relater. If $relater is set, use that to determine the $relatee to index by. My problem with that is that it's possible to specify the criteria such that some of the matching relationships don't have $relater as either the requester_id or the requestee_id. The returned array might then be unpredictably indexed, possibly leading to weird, hard-to-track-down bugs.

What do folks think? How should the function determine the uid of the relater: by inspecting the criteria, which is a bit cryptic but always safe, or by accepting a parameter, which is more explicit but riskier?

prfctns6@gmail.com’s picture

Well, I'll just have a big ol' conversation with myself here. :-)

I realized that there can be more than one relationship between 2 individuals, so returning a flat array indexed by relatee would only return one of their relationships, selected somewhat arbitrarily. That's obviously unacceptable, so now I'm thinking of having 3 API functions:

 user_relationships_count_relationships($criteria = array());
 user_relationships_load_relationships($criteria = array());
 user_relationships_load_relationships_by_relatee($criteria = array(), $relater = -1);

ur_count_relationships() just returns a count, as it does already. ur_load_relationships() returns a flat array with an arbitrary index. ur_load_relationships_by_relatee() returns an array indexed by $relatee relative to $relater. Each element of the array is itself an array of the relationships that exist between $relater and $relatee. All relationships that match the criteria but don't involve $relater are returned in the array indexed by $relatee=-1, so as not to conflict with any valid user id.

I think this works ok., and is fairly comprehensible. Anybody else want to chime in on this conversation? :-)

prfctns6@gmail.com’s picture

Status: Active » Needs review
FileSize
4.47 KB

Here's a patch that implements the API as described in update #2 above.

prfctns6@gmail.com’s picture

Status: Needs review » Fixed

In the absence of any objections, I've committed the changes to the API.

I also updated the criteria for counting/loading relationships such that the values of uid, uid1, uid2, rid, requester_id, requestee_id and rtid can be scalars or arrays; if they're arrays, the functions match relationships where the appropriate column matches any element in the array.

Anonymous’s picture

Status: Fixed » Closed (fixed)

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