I am working on some code to programmatically create user relationships (using Drupal 6.14). I am passing an array of userIDs to the user_relationships_load() API call but it is returning an incorrect number of results. At this point, I believe this is a SQL issue..

Example code:

$users = array(1,4,5,6,7,10,11);
$ur = user_relationships_load(
  array(
    'user' => $users,
  )
);

In the user_relationships_load() function after the following statement is executed I dumped the $query array:

$query = _user_relationships_generate_query($param, $options);
Array
(
    [query] => SELECT DISTINCT ur.rid,ur.*,urt.* FROM {user_relationships} ur INNER JOIN {user_relationship_types} urt USING ( rtid ) WHERE (ur.requester_id IN (%d,%d,%d,%d,%d,%d,%d) OR ((ur.approved <> 1 OR urt.is_oneway <> 0) AND ur.requestee_id IN (%d,%d,%d,%d,%d,%d,%d))) 
    [count] => SELECT COUNT(DISTINCT rid) AS count FROM {user_relationships} ur INNER JOIN {user_relationship_types} urt USING ( rtid ) WHERE (ur.requester_id IN (%d,%d,%d,%d,%d,%d,%d) OR ((ur.approved <> 1 OR urt.is_oneway <> 0) AND ur.requestee_id IN (%d,%d,%d,%d,%d,%d,%d)))
    [arguments] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => 4
                    [2] => 5
                    [3] => 6
                    [4] => 7
                    [5] => 10
                    [6] => 11
                )

            [1] => Array
                (
                    [0] => 1
                    [1] => 4
                    [2] => 5
                    [3] => 6
                    [4] => 7
                    [5] => 10
                    [6] => 11
                )

        )

)

When I execute the SQL in my mysql client application I get 42 results, but when the SQL is executed via Drupal, I get 6.

I think I tracked the issue down to the db_query() function in database.mysql-common.inc. See:

$query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);

Before the above statement is executed:

SELECT DISTINCT ur.rid,ur.*,urt.* FROM user_relationships ur INNER JOIN user_relationship_types urt USING ( rtid ) WHERE (ur.requester_id IN (%d,%d,%d,%d,%d,%d,%d) OR ((ur.approved <> 1 OR urt.is_oneway <> 0) AND ur.requestee_id IN (%d,%d,%d,%d,%d,%d,%d)))

After the preg is executed:

SELECT DISTINCT ur.rid,ur.*,urt.* FROM user_relationships ur INNER JOIN user_relationship_types urt USING ( rtid ) WHERE (ur.requester_id IN (1,1,0,0,0,0,0) OR ((ur.approved <> 1 OR urt.is_oneway <> 0) AND ur.requestee_id IN (0,0,0,0,0,0,0)))

It looks like the arguments are not inserted correctly into the SQL statement. Please advise. Thanks!

-Eric

Comments

alex.k’s picture

Status: Active » Closed (works as designed)

I don't think the function was designed to support multiple uids in the 'user' parameter