Last updated 12 June 2012. Created on 12 June 2012.
Edited by blueminds. Log in to edit this page.

<< sdbo project page

Basic usage

To start it is enough to define Drupal schema, make sure Drupal has created the
table and extend the SDBObject class. Make sure the schema defines primary key
named "id". By doing so the SDBObject will take care of this attribute.

// Drupal schema for example objects.
$schema['sdbo_example_objects'] = array(
  'description' => 'SDB Example objects table.',
  'fields' => array(
    'id' => array(
      'description' => 'Object id',
      'type' => 'serial',
      'not null' => TRUE,
      'unsigned' => TRUE),
    'created' => array(
      'description' => 'Timestamp when the object was created',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0),
    'title' => array(
      'description' => 'Object title',
      'type' => 'varchar',
      'length' => 124,
      'not null' => TRUE,
      'default' => ''),
    'body' => array(
      'description' => 'Object body',
      'type' => 'text',
      'not null' => FALSE),
  'primary key' => array('id'),

 * Example SDBObject.
class SDBExampleObject extends SDBObject {

  // Persisted attributes as defined in the schema.
  // Note that you do not need care about id attribute.
  public $created;
  public $title;
  public $body;

  const TABLE = 'sdbo_example_objects';

  function getTable() {
    return self::TABLE;

Create new object

$object = SDBFactory::initInstanceOf('SDBExampleChild', $form_state['values']);

Update existing object

// Load from db.
$object = SDBFactory::loadInstanceOf('SDBExampleChild',
  array('id' => $form_state['values']['id']));
// Set new value for title attribute.
$object->title = $form_state['values']['title'];
// Call save operation - the internal logic will handle updates/inserts.

Update existing object - more convenient way

// Init object. Make sure $form_state['values'] contains primary key of the
// object that will be updated.
$object = SDBFactory::initInstanceOf('SDBExampleChild', $form_state['values'],

// Call save operation - by setting 4th parameter of initInstanceOf to TRUE you
// explicitly tell the object is persisted and therefore SDBO will go for
// update.

Delete object

// Load from db.
$object = SDBFactory::loadInstanceOf('SDBExampleChild',
  array('id' => $form_state['values']['id']));
// Call delete operation. This will query database with delete query as well as
// will remove the object from internal cache registry, so the object is no more
// available in the SDBFactory registry.

Advanced usage

For advanced usage see the example module. The module provides five real life
examples that cover all the features listed above. You can access them on /sdbo-example path. It contains additional documentation and code examples that actually get executed so you can follow changes in the db.

Looking for support? Visit the forums, or join #drupal-support in IRC.