Inheritance

Table of Contents

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.

  1. One table one class
  2. 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.

1) The current version of EZPDO does not support automatic column aggregation for many-class-one-table mapping, so this has to be done manually, but it has been put into the feature request list.

6 user comments

  1. ez-pz on February 20th, 2006:

    Hi, small type error.

    ‘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 ***primitve*** variables to be mapped’

  2. ezpdo4php on February 20th, 2006:

    thanks. now fixed.

  3. Silvio Fonseca on May 6th, 2006:

    Using the one-table-one-class strategy, if I perform a find with a Base object, will I receive instances of all subclasses also (Subclass_A and Subclass_B in the example above)?

    Thanks

  4. Sergey on December 4th, 2006:

    One Table Many Classes - don’t works now, see
    http://www.ezpdo.net/bugs/index.php?do=details&id=205

  5. Greg on June 14th, 2007:

    What about table inheritance? It seems pointless to extend from a base object and then require all the base objects persisted fields to be in the inherited objects table. It is unnecessary duplication of data.

  6. ezpdo4php on June 15th, 2007:

    isn’t that all depending on what you have in the base object? the programmer after all decides what to put in a base class. no?

Post your comments

XHTML: tags you can use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Couldn't find your convert utility. Check that you have ImageMagick installed.