Overview
Inheritance mapping is an important aspect in any object-relation mapping tools. EZPDO has great flexibility in this regard.
Strategies
Two strategies can be used for inheritance mapping in EZPDO.
- One table one class
- One table many classes
By default, one-table-one-class is used.
One Table One Class
Simply put, with the one-table-one-class (also known as table-per-class) strategy, each class is mapped to a distinct database table.
Using the class-level @orm tag, you can associate a class with a database table. If you don’t, the class is mapped to a table of the same name in the default database (specified in option default_dsn in the configuration file). So, by default, classes are persisted on a table-per-class basis. That is, each class is mapped to a unique database table.
A subclass inherits all persistable variables (data members that are declared with the @orm tag) from its base class. For example, suppose we have a class hierarchy like this
Base
(a,b)
|
+--------+---------+
| |
Subclass_A Subclass_B
(c,d) (e,f,g)
In the parentheses under the class name are the persistable primitive variables declared in the class. For example, class Base has two persistable vars, $a and $b.
By inheritance, Subclass_A has four persistable variables in total, of which $a and $b are inherited from class Base, and $c and $d are declared in its own. Similiarly, Subclass_B has five persistable variables, $a, $b, $e, $f and $g.
In terms of table structure, the table for a class contains all the columns to be associated to the persistable primitive variables of the class, either inherited from its base classes or declared in its own.
One Table Many Classes
It is also possible to persist many classes into one table.
For the same class hierarchy above, if we specify the class-level @orm tags to map all classes to the same table, we can persist all classes into one table. For example, we may write
/** * @orm mytable */ class Base { // ...... } /** * @orm mytable */ class Subclass_A extends Base { // ...... } /** * @orm mytable */ class Subclass_B extends Base { // ...... }
This way, all three classes above will be persisted into one table (mytable).
To persist more than one classes into one table, one needs to make sure that the table is created in such a way that it contains all the columns for all persistable primitive variables to be mapped to1). For the above example to work, the table (mytable) must have columns for all primitive variables ($a, $b, $c, $d, $e, $f and $g) in addition to the oid column.
Under the one-table-many-class strategy, rows for base class objects may have some columns left empty if they are columns for the variables declared in the subclasses.