Skip navigation.
Home
That which cannot be rendered in binary is by definition a delusion
 

An ORM for Drupal

I have been working on ORMs for years. At first I figured Drupal would be resistant to ORM, but after getting dirty with the schema, I think there are a lot of design patterns in Drupal that make it relatively easy to wrap in an ActiveRecord and Domain pattern.

The Active Record module below has classes that enable ORM patterened data retrieival. The classes involved are:

  • Activerecord: represents a single row in the databse
  • Activerecord_table: the gateway to the database. Has schema data.
  • Activerecord_node_domain: A composite business class that loads and saves tables related to a node. Presumes a "detail" table, keyed by vid, to the node.
  • Activerecord_node_table: A specialized table class for node records: uses node_save() in its save routine.
  • Activerecord_schema: A class representing metadata on a table row.
  • Activerecord_crud and Activerecord_sql: library classes that do Sql creation for other classes.
  • Activerecord_loader: Loads a set of records from a YAML file.

these classes use the db libraries (db_query, db_fetch_array, db_fetch_object) and store underlying field data in stdClass objects, and so interoperate with the Drupal structure. Schema information is taken from the tables directly (via Describe).

This module is VERY BETA. Feel free to try it out but be wary of applying it to commercial work.

This class system will pre-empt certain database hooks, specifically, hook_insert, hook_delete, and hook_update. You should still write those hooks, though; you can if you want route them through an Activerecord but you can't assume that other processes won't need them.

There is a set of unit tests in the library, accessible off the Admin menu (Active Record/Test). They are a good illustration of the various methods of the libraries.

AttachmentSize
activerecord.zip21.07 KB

Post new comment

  • Allowed HTML tags: <a> <p> <span><small> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options