This project is not covered by Drupal’s security advisory policy.

This is a utility / support module that simplifies SELECT, INSERT, UPDATE and DELETE operations for tables with a simple primary key (either an auto_increment key or a key defined in the sequences table).

The functions are as follows:

$object = crud_get('tablename', $id);
$object = crud_find('tablename', array('field' => 'value', 'field2' => 'value2', ...));
$id = crud_save('tablename', $array_or_object);
crud_delete('tablename', $id);

Some features:
* Automatically serializes objects and arrays
* Caches table information for fast acces

Limitations:
* MySQL support only

Example usage:

Given the table definition:

CREATE TABLE my_table (
  id int not null auto_increment primary key,
  my_field varchar(100),
  my_other_field varchar(100),
  my_complex_data text,
);

And given a data array:

$edit = array(
  'my_field' => 'data',
  'my_other_field => 'more_data',
  'my_complex_data' => array(1,2,3,4,5),
);

You can use this code:

  // Create a new record:
  $edit['id'] = crud_save('my_table', $edit);

  // Update an existing record:
  crud_save('my_table', $edit);

  // Retrieve data:
  $data = crud_get('my_table', $id);

instead of this:

  if ($edit['id']) {
    db_query('UPDATE {my_table} SET my_field = "%s", my_other_field = "%s", my_complex_data = "%s" WHERE id = %d',
       $edit['my_field'], $edit['my_other_field'], serialize($edit['my_complex_data']), $edit['id']);
  }
  else {
    db_query('INSERT INTO {my_table} (my_field, my_other_field, my_complex_data) VALUES ('%s', '%s', '%s')',
       $edit['my_field'], $edit['my_other_field'], serialize($edit['my_complex_data']));
    $id = mysql_insert_id();
  }

Project information

Releases