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.

1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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