views:

233

answers:

2

I'm moving towards an ORM for my codeigniter application and have chosen datamapper. However, in the rules section, it states the following:

A joining table must exist between each related normal tables, regardless of the type of relationship.

I have dozens of tables that are in a one->many relationship. Does this mean that I have to create intermediate (joining) tables between each of them as if they were many-many?

A: 

Yes, unfortunately, it does mean that. This is one of the reasons why I had to change my DB design

+1  A: 

No, actually, the model that has a $has_one relationship set will have a field ending in _id, which refers to the object with the $has_many relationship set.

So, for example:

class Recipe extends DataMapper {
    var $has_many = array('product', 'rating', 'recipe_category');
    var $has_one = array('recipe_source', 'user');
    (...)
}

class Recipe_Source extends DataMapper {
    var $has_many = array('recipe');
    (...)
}

In this case, DataMapper made a table recipes with a recipe_source_id column, and a recipe_sources table that required no extra fields.

Now, for many-to-many relationships, a join table will be created, and it follows a strict convention.

The join table name will be the two plurals of the joined models, separated by an underscore, in alphabetical order.

So, using this model:

class Product extends DataMapper {
    var $has_one = array('cheese_style', 'butter_style', 'cheese_flavor');
    var $has_many = array('product_size', 'recipe');
    (...)
}

Now, I end up with a join table in my database called products_recipes.

This is how it is handled by the DMZ DataMapper library (http://www.overzealous.com/dmz/), which is supposed to be a drop-in replacement for the old stensi DataMapper library, so I'm going to assume the conventions are the same.

Even so, I highly recommend making the switch to DMZ.

Alan Christopher Thomas