Hopefully someone can shed light on this issue either with an example or perhaps with some suggested readings. I am wondering what is the best design approach for modeling tables after their class hierarchy equivalents . This can best be described with an example:
abstract class Card{ private $_name = ''; private $_text = ''; } class MtgCard extends Card{ private $_manaCost = ''; private $_power = 0; private $_toughness = 0; private $_loyalty = 0; } class PokemonCard extends Card{ private $_energyType = ''; private $_hp = 0; private $_retreatCost = 0; }
Now that I have modeled tables for synchronization with this class hierarchy, I went with something very similar:
TABLE Card id INT, AUTO_INCREMENT, PK name VARCHAR(255) text TEXT TABLE MtgCard id INT, AUTO_INCREMENT, PK card_id INT, FK(card.id) manacost VARCHAR(32) power INT toughness INT loyalty INT TABLE PokemonCard id INT, AUTO_INCREMENT, PK card_id INT, FK(card.id) hp INT energytype ENUM(...) retreatcost INT
The problem I encountered is figuring out how to associate each Card entry with an entry containing its data from the corresponding table. In particular, how to determine which table I should look for.
Should I add a VARCHAR column to Card to keep the name of the linked table? This is the only solution that my peers have come to, but it seems too "dirty." Saving the design extensibly is the key here, making it easy to add new subclasses .
If someone could provide an example or resources showing a clean way to mirror the class / table hierarchy, that would be very helpful.
source share