[drupal-devel] Random: Drupal Relationships

Morbus Iff morbus at disobey.com
Fri Mar 11 23:53:27 UTC 2005


In working with taxonomy, I'm now leaning toward the following as a 
replacement for term_node (I'm using words here for readability, but 
assume all "id" columns are, in fact, numbers):

    tid | uid    | type    | id
   -----------------------------
   cat  | morbus | image   | 67
   cat  | moshe  | node    | 17
   dog  | killes | comment | 129
   cool | dries  | user    | 1

which would also satisfy node/899 as well. But, my work with librarian 
tech and the LibDB project (now halted) has resurfaced because of the 
above. The "type / id" mentality was used in LibDB's relationship table, 
but was expanded to include a generic two way relationship. I'd like to 
bounce the same idea for Drupal core and see what you think.

Here is the table structure and example data (again, assume that if an 
ID makes sense in the column, it'd be there, and that "f" is "from", "r" 
is "relationship" and "t" is "to"). I've include explanatory sentences 
under each column:

  f_type | f_data | r_type | r_data | r_who | t_type | t_data
  ------------------------------------------------------------
  term   | cat  | taxo   |        | morb  | image  | 67
   "taxonomy relationship between term cat and image 67, by morbus"

  term   | dog  | taxo   |        | moshe | node   | 17
   "taxonomy relationship between term dog and node 17, by moshe

  user   | morb | user   | friend | moshe  | user   | dries
   "user relationship: moshe asserts that
    user morbus is a friend of user dries"

  user   | jonbob | user   | lover | morb  | user   | dries
   "user relationship: morbus asserts that
    user jonbob is a lover of user dries"

  user   | walkah | rating | 6     | walkah | node  | 16
  user   | morbus | rating | 10    | morbus | user  | dries
   "user walkah has given node 16 a rating of 6"
   "user morbus has given user dries a rating of 10"

And so on and so forth. In essence:

  * anything can relate to anything else.
  * the types of relationship are defined by the module.
  * relationships may have additional data (rating, "friend").

The above is pretty powerful (but at what cost - who knows a lot about 
gigantic tables - is the lookup insanely bad?), and is flexible enough 
to handle a lot of features that would normally require overhauling 
existing tables or creating new ones entirely. Two recent modules I've 
seen, for example, serve the following purpose:

  * different URLs use different templates (sections module, I think).
  * nodes can be in a child/parent rel (node_relativity, I think).

I'm assuming they both do this magic with new tables. But, what about:

  url   | /forums | sections   |        | morbus | template  | happy.tpl
  node  | 16      | relativity | child   | morbus | node      | 19
  node  | 18      | relativity | inspired | morbus | node      | 25

And so on.

-- 
Morbus Iff ( you, me, eropuri? aawwwwwWWWw yYeahahhHHAhhh )
Technical: http://www.oreillynet.com/pub/au/779
Culture: http://www.disobey.com/ and http://www.gamegrene.com/
icq: 2927491 / aim: akaMorbus / yahoo: morbus_iff / jabber.org: morbus



More information about the drupal-devel mailing list