Opened 9 years ago
Closed 7 years 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:
- Run a query just to get IDs that match a post query -
SELECT ID FROM $wpdb->posts
- Populates the cache for individual post objects (
_prime_post_caches()
), using a single querySELECT * FROM $wpdb->posts WHERE ID IN (...)
- Populate the results array like this:
foreach ( $ids as $id ) { $found[] = get_post( $id ); }
, whereget_post()
(in a roundabout way) grabs the post data from the cache and passes it toWP_Post::__construct()
, which in turn populates a newWP_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
@
9 years ago
- Keywords needs-patch added
- Milestone changed from Future Release to Under Consideration
- Version set to 1.0
#2
@
9 years ago
- Keywords early added
- Milestone changed from Under Consideration to Future Release
This would be sweet.
#5
@
7 years 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/
This is a must-have IMO, especially for the eventuality of RESTful API endpoints talking to actual PHP objects.