Skip to:
Content

BuddyPress.org

Opened 5 years ago

Closed 22 months ago

#6358 closed enhancement (maybelater)

Query methods should return arrays of BP objects

Reported by: boonebgorges Owned by:
Milestone: Priority: normal
Severity: normal Version: 1.0
Component: Core Keywords: needs-patch, early, trac-tidy-2018
Cc:

Description

We have nice classes for most of our data types - BP_Groups_Group, BP_Activity_Activity, and so on - and when we fetch individual items, we usually handle them as objects that instantiate these classes. When querying for *multiple* items, though, we basically return the results of $wpdb->get_results(), which is an array of stdClass objects. (BP_Activity_Activity::get(), etc.)

It would be better if these query methods returned arrays of BP objects - better for internal use (we could start using, you know, actual OOP techniques in a reliable way), better for centralizing cache and invalidation logic, better for a consistent developer interface, etc.

The trick is to do this in a way that doesn't require a single database query for each object. Here we should take a page from WP, which does the following for post queries:

  1. Run a query just to get IDs that match a post query - SELECT ID FROM $wpdb->posts
  2. Populates the cache for individual post objects (_prime_post_caches()), using a single query SELECT * FROM $wpdb->posts WHERE ID IN (...)
  3. Populate the results array like this: foreach ( $ids as $id ) { $found[] = get_post( $id ); }, where get_post() (in a roundabout way) grabs the post data from the cache and passes it to WP_Post::__construct(), which in turn populates a new WP_Post object without requiring another DB query.

We already do a pretty good job with (a) and (b), at least in bp-activity and bp-groups. So it should be a reasonable task to do (c) too. Let's!

Change History (6)

#1 @johnjamesjacoby
5 years ago

  • Keywords needs-patch added
  • Milestone changed from Future Release to Under Consideration
  • Version set to 1.0

This is a must-have IMO, especially for the eventuality of RESTful API endpoints talking to actual PHP objects.

#2 @DJPaul
5 years ago

  • Keywords early added
  • Milestone changed from Under Consideration to Future Release

This would be sweet.

#3 @boonebgorges
4 years ago

In 10198:

Introduce cache support for xprofile fields.

xprofile_get_field() will now look in the cache before returning a
BP_XProfile_Field object (via the new BP_XProfile_Field::get_instance()),
and will populate the cache if necessary. It's strongly recommended to use
xprofile_get_field() when fetching individual existing fields, instead of
creating new BP_XProfile_Field objects directly.

BP_XProfile_Group::get() now leverages the field cache too. Instead of a
SELECT * query, it fetches a list of matching field IDs SELECT id, and
populates the full field objects from the cache, if available. The fields
property of BP_XProfile_Group objects is now an array of BP_XProfile_Field
objects, not stdClass. See #6358.

Props boonebgorges, r-a-y.
See #6638.

#4 @DJPaul
3 years ago

  • Component changed from API to Core

#5 @DJPaul
22 months ago

  • Keywords trac-tidy-2018 added

We're closing this ticket because it has not received any contribution or comments for at least two years. We have decided that it is better to close tickets that are good ideas, which have not gotten (or are unlikely to get) contributions, rather than keep things open indefinitely. This will help us share a more realistic roadmap for BuddyPress with you.

Everyone very much appreciates the time and effort that you spent sharing your idea with us. On behalf of the entire BuddyPress team, thank you.

If you feel strongly that this enhancement should still be added to BuddyPress, and you are able to contribute effort towards it, we encourage you to re-open the ticket, or start a discussion about it in our Slack channel. Please consider that time has proven that good ideas without contributions do not get built.

For more information, see https://bpdevel.wordpress.com/2018/01/21/our-awaiting-contributions-milestone-contains/
or find us on Slack, in the #buddypress channel: https://make.wordpress.org/chat/

#6 @DJPaul
22 months ago

  • Milestone Awaiting Contributions deleted
  • Resolution set to maybelater
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.