Skip to:
Content

BuddyPress.org

Changeset 7324


Ignore:
Timestamp:
07/27/2013 06:09:57 PM (12 years ago)
Author:
boonebgorges
Message:

Enforce DISTINCT in BP_Activity_Activity::get() queries

Previously, we've never really needed to state DISTINCT explicitly in
BP_Activity_Activity::get(), because the kinds of queries we ran would
naturally result in the exclusion of duplicates. The introduction of
the meta_query parameter in 1.8 - and in particular, the ability to
introduce more than one meta_query clause separated by the inclusive
OR relation - uncovered the bug, as single activity items were matching
more than one clause in the meta_query and thus being returned in the
results more than once.

Fixes #5118

Location:
branches/1.8
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.8/bp-activity/bp-activity-classes.php

    r7228 r7324  
    156156
    157157        // Select conditions
    158         $select_sql = "SELECT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name";
     158        $select_sql = "SELECT DISTINCT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name";
    159159
    160160        $from_sql = " FROM {$bp->activity->table_name} a LEFT JOIN {$wpdb->users} u ON a.user_id = u.ID";
     
    248248        }
    249249
    250         $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(a.id) FROM {$bp->activity->table_name} a {$index_hint_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $where_sql, $sort );
     250        $total_activities_sql = apply_filters( 'bp_activity_total_activities_sql', "SELECT count(DISTINCT a.id) FROM {$bp->activity->table_name} a {$index_hint_sql} {$join_sql} {$where_sql} ORDER BY a.date_recorded {$sort}", $where_sql, $sort );
    251251
    252252        $total_activities = $wpdb->get_var( $total_activities_sql );
  • branches/1.8/tests/testcases/activity/class.BP_Activity_Activity.php

    r7155 r7324  
    7676    /**
    7777     * @group get
     78     * @group meta_query
    7879     */
    7980    public function test_get_with_meta_query() {
     
    9293        $ids = wp_list_pluck( $activity['activities'], 'id' );
    9394        $this->assertEquals( $ids, array( $a1 ) );
     95    }
     96
     97    public function test_get_with_meta_query_two_clauses_with_or_relation() {
     98        $now = time();
     99        $a1 = $this->factory->activity->create( array(
     100            'recorded_time' => date( 'Y-m-d H:i:s', $now ),
     101        ) );
     102        $a2 = $this->factory->activity->create( array(
     103            'recorded_time' => date( 'Y-m-d H:i:s', $now - 60 ),
     104        ) );
     105        $a3 = $this->factory->activity->create( array(
     106            'recorded_time' => date( 'Y-m-d H:i:s', $now - 120 ),
     107        ) );
     108        bp_activity_update_meta( $a1, 'foo', 'bar' );
     109        bp_activity_update_meta( $a2, 'foo', 'bar' );
     110        bp_activity_update_meta( $a1, 'baz', 'barry' );
     111
     112        $activity = BP_Activity_Activity::get( array(
     113            'meta_query' => array(
     114                'relation' => 'OR',
     115                array(
     116                    'key' => 'foo',
     117                    'value' => 'bar',
     118                ),
     119                array(
     120                    'key' => 'baz',
     121                    'value' => 'barry',
     122                ),
     123            ),
     124        ) );
     125
     126        $ids = wp_list_pluck( $activity['activities'], 'id' );
     127        $this->assertEquals( array( $a1, $a2 ), $ids );
     128        $this->assertEquals( 2, $activity['total'] );
    94129    }
    95130
Note: See TracChangeset for help on using the changeset viewer.