Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm using an ORM (Propel) and PHP to create Objects and store them in a database. So, I'm facing a recurrent problem and I'm pretty sure this happened to someone before.

My data model looks something like this:

Strategy pattern situation

So class A needs a reference to something from the hierarchy of Clase Parent (abstract class), this is not the problem. The idea behind this solutions is that in the future it would be very easy to create new strategies. But remember I said I'm using an ORM to store info in the database?

Well, the database has 3 tables: A_table, B_table and C_table. So here comes the situation:

A_table needs to be related to the strategy. Whats the best way to go?

  1. Create a strategy type reference table, assosiate a type to the A_table and have a relation_id thats just an intenger with a value (no referential integrity whatsoever)
  2. Create one field per strategy in the A_table, and allow them to be null

Really looking forward for opinions and why its best one or the other.

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

I faced a similar problem, using NHibernate as my ORM. In that case, I created a separate table for Parent with just an Id column in it. Then I created one-to-one relationships between B and Parent and C and Parent. So, for every record in B or C there is a corresponding record in Parent. You do this by making your primary keys in B and C also foreign keys referencing Parent. Then I could create a normal foreign key relationship between A and Parent.

That has 2 advantages:

  1. It's easy to add new strategies
  2. It's easy to add properties to the Parent class, if necessary

Of course, you end up with one more table, but I found that the ORM made it all transparent.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.