A Path Less Taken

Breaking with convention in a very conventional fashion. Powered by WordPress

"What would you attempt to do if you knew you could not fail?"
Dr. Robert Schuller

Monday, March 1, 2010

Category: PHP Development Tags: Author: JJ 0 Comments

Object Relational Mapping (ORM) tools allow developers to interact with databases through objects. The basic object in the typical solution is the model. The model is essentially an object representation of a single table in the database. In fact, implementations that subscribe to the activerecord design pattern will often view the model as an object representation of a single row of a specific table. Since the target data store in most cases is an RDBMS (excluding the recent surge in distributed Document based DB’s) the typical ORM needs some way to represent foreign key relationships in an object oriented way. To facilitate this the field has evolved a set of conventions or names for foreign key (FK) relationships in the DB based on the object’s (table’s) role in the given index. Here is a brief but hopefully useful explanation of four relationship types.

  • Belongs-To – This is the most basic form of a FK representation in an ORM. A field in a model is designated as type Belongs To when that field has is a FK that references the primary key (PK) in another model (table). I say model since the Belongs To is used to describe the behavior of one model that contains a FK index in relation to another model that is the target of that FK index. The typical default behavior would be for the field to be named by using the target model’s name joined to the target model’s PK field name by an underscore such as author_id. The assumption in this example being that the author table would have a PK field named id.
  • Has-One – This is the other side of the Belongs-To convention. This tells the model that another model is using this models PK as the target for its FK. The model can use this relationship to manage cascading deletes, joined selections, etc. The implication is that the model expects there to be one and only one FK in the remote model (table) attached to each record in this model (table).
  • Has-Many – This is the same as Has-One except now the model expects there to be one or more FK’s in the remote model (table) attached to each record in this model (table).
  • Many-To-Many – Although you might assume this is similar to Has-Many, it is actually more akin to Belongs-To. This convention represents a FK relationship from this model (as with Belongs To) to an intersection table that in turn joins it to another model. The typical implementation contains a Through property that defines the name of the intersection table. By default this table will typically be named by joining the name of each model (table) connected to it with an underscore character.

That is all there is to it. Although this is a simple explanation of these four relationship types, I think you can begin to see how the ORM will rely on each to execute desirable behaviors as you interact with your database through model objects. If you have any comments or corrections to the above please let me know.

No comments found. Please enter a comment if you have a question or contribution.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">