[buddypress-trac] [BuddyPress Trac] #6358: Query methods should return arrays of BP objects
buddypress-trac
noreply at wordpress.org
Wed Apr 8 16:56:28 UTC 2015
#6358: Query methods should return arrays of BP objects
--------------------------+----------------------------
Reporter: boonebgorges | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Future Release
Component: API | Version:
Severity: normal | Keywords:
--------------------------+----------------------------
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:
a. Run a query just to get IDs that match a post query - `SELECT ID FROM
$wpdb->posts`
b. Populates the cache for individual post objects
(`_prime_post_caches()`), using a single query `SELECT * FROM $wpdb->posts
WHERE ID IN (...)`
c. 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!
--
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/6358>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac
More information about the buddypress-trac
mailing list