DrupalORM provides a programmatic ORM interface to nodes and their corresponding CCK fields.

Assume you have a content type called "page" with the following node and CCK fields...

  • Node title
  • Node body
  • CCK Field "field_desc"
  • CCK Node reference "field_ref" which points to content type "story"

You could query "page" content type in the following ways...

<?php
orm
('Page')->find(array(
 
'Page.nid' => 34
));
?>
<?php
$nodes
= orm('Page')->find(array(
 
'Page.field_desc' => 'foobar',
 
'Page.title' => 'hello world'
));
// $nodes will be an OrmNodeCollection object
print $nodes->{0}->title;
print
$nodes->{1}->title;
// Or you can also do
foreach ($nodes as $node) {
  print
$node->title;
}
?>

Assuming the corresponding nodes exist, both of these will return an OrmNodeCollection object which contains a set of PageNode objects. Also, any story nodereferences will be populated recursively....

<?php
$nodes
= orm('Page')->find(array(
 
'Page.title' => 'bup'
));
// Prints title of corresponding Story node which is defined via CCK nodereference.
print $nodes->{0}->FieldRef->{0}->title;
// You can also do this
$nodes->{0}->FieldRef->{0}->body = 'Hello World';
// Now save the changes. There are many ways to do it...
// Method 1
$nodes->saveAll();
// Method 2
$nodes->{0}->save();
// Method 3
$nodes->{0}->FieldRef->{0}->save();
// Method 4
$nodes->{0}->FieldRef->saveAll();
?>

Take note that content type names are converted to toggle case. So if you had a content type named "vacation_picture", you would access it like so...

<?php
$nodes
= orm('VacationPicture')->find(...);
?>

If you wish to search or save resources without recursion, you can do...

<?php
$nodes
= orm('Page')->find(array(
 
'Page.title' => 'bup'
), array(
 
'recursive' => FALSE
));
$nodes->{0}->body = 'Hello World';
$nodes->{0}->save(array(
 
'recursive' => FALSE
));
?>

DrupalORM can also create and delete node objects in an OOP like fashion.

An example of deletion...

<?php
$nodes
= orm('Page')->find(array(
 
'Page.nid' => 12
));
$nodes->{0}->delete();
// Or to delete a set of nodes
$nodes->deleteAll();
?>

An example of creation...

<?php
$newNode
= orm('Page')->create();
$newNode->setTitle('hello world');
$newNode->body = 'foobar';
$newNode->save();
?>

Project Information

Downloads