Fetching CCK nodes spanning multiple Content Types

Events happening in the community are now at Drupal community events on www.drupal.org.
KingMoore's picture

CCK: 6.x-2.1
Drupal: 6.8

I am working on a module that needs to fetch sets of nodes based on various criteria (I don't think the criteria isn overly important for this discussion). The content nodes may span all content types installed on the site. I need to fetch a list of all the nodes matching my requirements and all of their fields/content.

I need this basic functionality:

$result = db_query('SELECT * FROM {node} WHERE #my conditions are met#');
while($row = db_fetch_object($result)){
 
  $node = node_load($row->nid);

  ... do stuff ...

}

the above code:

  1. fetches all nodes from the node table that match my criteria
  2. for each node, I then load all the data for that node using a node_load()

The problem is, node_load() is expensive and if my result sets are getting hundreds of rows (or more), having to do hundreds of node_load()s could get quite painful. I'm pretty sure Views solves this problem by building custom queries that are more efficient than doing node_load()s for each node, but the idea of me building a robust query builder to handle all scenarios is rather daunting (and I would like to avoid it!).

Is there any sort of query building API, or code I can leverage that will help me provide the functionality above, without doing a node_load() for each record? Or am I simply doomed to eat the performance cost of doing N node_load()s?

As a side note, I just joined the CCK group as the 666th member.

Comments

Use views

kswan's picture

Views is the query building API. You could make your query in the Views UI or using the Views API. Then in your module you could execute the Views query. There is some information that should help at http://groups.drupal.org/node/10129.

As a rough starting point, build your query in the Views UI then you should be able to do something like this:

<?php
$view
= views_get_view('view_name');
$view->execute();
foreach (
$view->result as $result) {
// Do stuff
}
?>

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week