18.10.2016 Views

Drupal 7 Module Development

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Working with Content<br />

'default' => '',<br />

),<br />

'created' => array(<br />

'description' =><br />

'The Unix timestamp when the artwork was created.',<br />

'type' => 'int',<br />

'not null' => TRUE,<br />

'default' => 0,<br />

),<br />

'changed' => array(<br />

'description' =><br />

'The Unix timestamp when the artwork was most recently saved.',<br />

'type' => 'int',<br />

'not null' => TRUE,<br />

'default' => 0,<br />

),<br />

),<br />

'unique keys' => array(<br />

'aid_vid' => array('aid', 'vid'),<br />

'aid' => array('aid')<br />

),<br />

'primary key' => array('aid'),<br />

);<br />

}<br />

return $schema;<br />

That looks like a lot of code, but it's really just another big <strong>Drupal</strong> array. The keys of<br />

the $schema array are the names of tables to be created. Each table is then defined as<br />

another nested array that defines the fields, indices, and other data about the table.<br />

Most are self-explanatory.<br />

See http://drupal.org/node/146843 for more information<br />

on the Schema API.<br />

Note that we're using an integer field called aid for our primary key. We also store<br />

the bundle that an artwork belongs to in a column called type (just like nodes),<br />

and we have a "version ID" field called vid. All entities can support versioning in a<br />

similar fashion to nodes, so let's build that in from the start.<br />

To store old revisions, we'll need another table as well. We'll call that table<br />

artwork_revision:<br />

$schema['artwork_revision'] = array(<br />

'description' =><br />

'Stores information about each saved version of an {artwork}.',<br />

[ 154 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!