I am coding in PHP two data objects in PHP, and they are very similar, but shall be kept in two separate tables. Therefore I am going to code them as a subclass of a superclass.
The superclass will hold most of the two classes’ duplicated functions for accessing the data from the database table. While specific functions/error checking/DB access functions of each class will be implemented in the subclass.
How do I realize this in the Database? I am thinking about the need of a table for the superclass.
Design one:
TableA and TableB will have a reference to TableSuperClass, and all commonly structured data will be linked to the TableSuperClass instead of the subclass (TableA and TableB) themselves. Giving TableA, TableB, TableSuperClass and CommonData. With TableA and TableB storing things specific only to class A/B.
Design two:
TableA and TableB will have mostly identical structure, with each having a few extra columns for their specific needs. There will be no table for the super class. The data that is common to both A and B will be stored in two separate tables. So essentially TableA, TableB, TableAData, TableBData
What are the advantage and disadvantage of the two designs?
Thanks
You’re referencing a technique called database polymorphism, sometimes class table inheritance, otherwise along those lines. Typically it’s implemented using your first design, but with 3 tables: a common table with common columns, and and a table for each “subclass” having it’s own unique columns. These are typically joined down with a type column on the common table, where each subclass would define it’s own type value in code and adhere to only using that value for joins against the common table and it’s table.