I'm trying to use the advertised ability of user_relationships_load to find a relationship created or updated within a range of times, and it doesn't seem to be working. Here's what's going on:

* I have an entry in {user_relationships} with, among other things, requester_id => 5 and updated_at => 1291018071.

* Following the documentation/comments in user_relationships_api.api.inc, I wrote the following call:

$theTest = user_relationships_load(array('requester_id' => 5, 'updated_at' => ">= 1290000000", 'updated_at' => "< 1300000000")); 

* This should, I believe, return at least the entry mentioned above. Instead, an empty/null array comes back.

To try to get some idea of what's going on, I put a print_r on the value of $query on line 372 of user_relationships_api.api.inc. It produces the following:

Array
(
    [query] => SELECT DISTINCT ur.rid,ur.*,urt.* FROM {user_relationships} ur INNER JOIN {user_relationship_types} urt USING ( rtid ) WHERE %s.%s = %d AND %s.%s < '%s' 
    [count] => SELECT COUNT(DISTINCT rid) AS count FROM {user_relationships} ur INNER JOIN {user_relationship_types} urt USING ( rtid ) WHERE %s.%s = %d AND %s.%s < '%s'
    [arguments] => Array
        (
            [0] => ur
            [1] => requester_id
            [2] => 5
            [3] => ur
            [4] => updated_at
            [5] => 
        )
)

That empty value in position 5 of the final array looks suspicious to me, but I don't know the code enough to go much beyond that.

So: Am I doing something wrong (always a possibility) or is there a bug here?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alex.k’s picture

Looking through the code, the problem seems to be in _user_relationship_process_query_argument() in the block starting at line 185:

  elseif (preg_match('/([<>=]{1,2})\s*(.+)/', $value, $matches)) {
    $marker = "'%s'";
    $value = $matches[3];

    if (is_numeric($value)) {
      $marker = '%d';
      $value = (int)$value;
    }

$matches[3] may be a problem... basically it's not extracting your timestamp value from the regular expression. It may have to be matches[2], not [3].

alex.k’s picture

By the way, array('requester_id' => 5, 'updated_at' => ">= 1290000000", 'updated_at' => "< 1300000000") has the same key updated_at for two values, hence the first of them is getting dropped.

jim_at_miramontes’s picture

I had the same thought, but all I was doing was repeating what was shown in the comments/documentation in user_relationships_api.api.inc (line 309):

  *   arguments will process operators as well using the syntax: array(col => '> {value}').
  *     example: show all relationships created in 2007
  *       $start_time = mktime(0,0,0,0,0,2007);
  *       $end_time = mktime(0,0,0,0,0,2008);
  *       user_relationships_load(array('created_at' => ">= {$start_time}", 'created_at' => '< {$end_time'}));

Is this documentation incorrect? This is where I got the idea in the first place.

Just for fun, I tried:

$theTest = user_relationships_load(array('requester_id' => 5, 'updated_at' => array(">= 1290000000", "< 1300000000"))); 

I'm not sure if this was supposed to work, but it didn't. The value of $query was:


Array
(
    [query] => SELECT DISTINCT ur.rid,ur.*,urt.* FROM {user_relationships} ur INNER JOIN {user_relationship_types} urt USING ( rtid ) WHERE %s.%s = %d AND %s.%s IN ('%s','%s') 
    [count] => SELECT COUNT(DISTINCT rid) AS count FROM {user_relationships} ur INNER JOIN {user_relationship_types} urt USING ( rtid ) WHERE %s.%s = %d AND %s.%s IN ('%s','%s')
    [arguments] => Array
        (
            [0] => ur
            [1] => requester_id
            [2] => 5
            [3] => ur
            [4] => updated_at
            [5] => >= 1290000000
            [6] => < 1300000000
        )
)

I'm currently handling my problem by doing a direct db_query into {user_relationships}, but if there's a way to do this with user_relationships_load(), I'd probably prefer it. On the other hand, if this isn't supposed to work, the documentation ought to be corrected to reflect the reality of the code.

alex.k’s picture

No, you are right that it's both a bug and incorrect documentation. I think that nobody ever hit that use case before. And I never noticed it was written this way :)

Replacing $matches[3] with $matches[2] might help if you want to give that a try. In any case, for your situation a direct query is better.

mrf’s picture

Issue tags: +6.x-1.1

Tagging for better tracking.

mrf’s picture

And more tagging.

mrf’s picture

Closed #988322: user_relationships_load not processing arguments with operators correctly, no results returned as a duplicate of this issue, but remember to give smthomas credit for his patch.

mrf’s picture

Version: 6.x-1.0-rc6 » 6.x-1.x-dev
Status: Active » Needs review
FileSize
719 bytes

Here's a patch for this, I just want to see what the bot has to say about this.

There should really be new tests included for testing any date-based parameters sent to user_relationships_load.

mrf’s picture

Ok, time time to stop for the day I think.

Here's another go at that one line patch...

mrf’s picture

Title: user_relationships_load not handling multiple arguments properly(?) » user_relationships_load not handling date-based parameters correctly

Status: Needs review » Needs work
Issue tags: -6.x-1.1, -user_relationships_load

The last submitted patch, user_relationship_load-dates-985672-9.patch, failed testing.

mrf’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, user_relationship_load-dates-985672-9.patch, failed testing.

mrf’s picture

Status: Needs work » Needs review
Issue tags: +6.x-1.1, +user_relationships_load
mrf’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Needs review » Patch (to be ported)
Issue tags: -6.x-1.1, -user_relationships_load

Fixed in 6.x-dev see http://drupalcode.org/project/user_relationships.git/commit/08e15a2

Moving to 7.x to confirm it is fixed there as well.

Berdir’s picture

I'm actually not sure if still support this syntax. Might need to be re-added. Although I'd prefer to build this on top of the DBTNG conditions API instead of strings like that.

sarci’s picture

Attached patch for D7.

sarci’s picture

Re-attached patch for D7 (removed unnecessary row).

sarci’s picture

Re-attached patch for D7 (added necessary row).

sarci’s picture

D7 patch with fix on array value.