views:

643

answers:

4

hi.

I create a custom import and export, at the moment as an external script (via bootstrap), i plan to create a module in a more generic fashion lateron.

I am building a frontend for nagios and for our host management and nagios configuration btw. Maybe it might become useful for other environments (networkmanagement)

Now i need to know how to get list of all nodes of type x?

I want to avoid direct SQL.

A suggestion i got was to make an rss and parse it but i acess the drupal db a dozen times to extract various nodes, so it feels strange to do a web request for one thing

So what i am looking for as newbie drupal dev is just a pointer to basic search module api for this task

TIA florian

+2  A: 

Why do you want to avoid SQL?

If you want to get info about what's in your db, like all the nodes of type x, the only way to get it, is through a SQL query, unless you have the data extracted already.

A query like

db_query("SELECT title, nid FROM {node} WHERE type = 'x';");

shouldn't be the thing that ruins your performance.

Edit:
The link you provided is a from Drupal 7, so you have to be be careful reading this. The reason is that in Drupal 7 it is not only possible to use db_query which basically is wrapper for the php functions mysql_query, pg_query. It's a bit different and using it, you wont have to use db_specific code. Anyways new in Drupal 7 is something that is a bit like an ORM. I haven't read about it in detail, but the idea is that you can build a query using commands on an object. This is probably what you are after. However, Drupal 7 is not ready at all for production sites. There are still a lot of critical issues and security issues. So this wont be a possibility for quite some time.

Edit 2:
If you want to get the node title and body, this is what you should do:

$type = 'x';
$query = db_query("SELECT r.nid r.title, r.body FROM {node} AS n 
                  LEFT JOIN {node_revisions} AS r ON r.nid = n.nid
                  WHERE type = '%s';", array($type));
$nodes = array();
while ($node = db_fetch_object($query)) {
    $nodes[$node->nid] = $node;
}

You can use db_fetch_array instead of db_fetch_object` if you want to extract arrays instead of objects from the db.

googletorp
okay i go with that if it's recommended and there is no api call for it. It's just because i have worked with ORMs a lot for about a year...So i did that but see only db_fetch_array( $query ) resp ..,.object which returns one array or object only.!?
groovehunter
okay i should get along with these docs. No need to comment this , thx . http://drupal.org/node/310072
groovehunter
these docs are confusing/wrong... now i am finally on the right track
groovehunter
A: 

Views is generally how you create database queries without writing them in Drupal, but this query is so simple I'm not sure it's worth the overhead of learning views, barely 5 lines after you've bootstrapped Drupal:

$nodes = array();
$results = db_query("SELECT nid FROM {node} WHERE type = '%s'", $type);
while ($result = db_fetch_object($result)) {
  $nodes[] = node_load($result->nid);
}
Scott Reynen
It's a very bad idea to do node_load on a large set of nodes, it'll run a large set of hooks and queries.
googletorp
i see. In my case there are about 20 and i need the body and some cck fields. So atm i keep my code and call node_load . thx also Scott. At least your hint with "db_fetch_object" worked, all other methods returned an empty set somehow i was too much banana . x;D
groovehunter
+1  A: 

Gotta use SQL do to this.

http://api.drupal.org/api/function/node_get_types/6

Node counts =

$node_types = node_get_types();

$type_count = array();

foreach ($node_types as $type) {
   $result = db_fetch_object(db_query('SELECT count(nid) AS node_count FROM {node} WHERE type = "%s"'), $type);
   $type_count[$type] = $result['count(nid)'];
}

print_r($type_count);

Nodes and their type:

$node_types = node_get_types();

$nodes = array();

foreach ($node_types as $type) {
   $result = db_query('SELECT nid, title FROM {node} WHERE type = "%s"'), $type);

   while ($node = db_fetch_object($result)) {
      $nodes[] = array('Type' => $type, 'Title' => $node->title);
   }
}

print_r($nodes);

Something like that. I am eating lunch so I didn't test that but I have done this before so it should work. Drupal 6.

Kevin
A: 

The migrate module may be of interest to you. It also supports drush so you can script things fairly easily.

Jeremy French