Object relational mapping - Relations - Inheritance - Column aggregation.php 2.67 KB
Newer Older
hansbrix's avatar
hansbrix committed
1 2 3 4 5 6 7
In the following example we have one database table called 'entity'. Users and groups are both entities and they share the same database table.

The entity table has a column called 'type' which tells whether an entity is a group or a user. Then we decide that users are type 1 and groups type 2.

The only thing we have to do is to create 3 records (the same as before) and add call the Doctrine_Table::setInheritanceMap() method inside the setUp() method.

<code type='php'>
lsmith's avatar
lsmith committed
8
class Entity extends Doctrine_Record {
hansbrix's avatar
hansbrix committed
9 10 11 12 13
    public function setTableDefinition() {
        $this->hasColumn('name','string',30);
        $this->hasColumn('username','string',20);
        $this->hasColumn('password','string',16);
        $this->hasColumn('created','integer',11);
lsmith's avatar
lsmith committed
14 15

        // this column is used for column
hansbrix's avatar
hansbrix committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
        // aggregation inheritance
        $this->hasColumn('type', 'integer', 11);
    }
}

class User extends Entity {
    public function setUp() {
        $this->setInheritanceMap(array('type'=>1));
    }
}

class Group extends Entity {
    public function setUp() {
        $this->setInheritanceMap(array('type'=>2));
    }
}
</code>

If we want to be able to fetch a record from the Entity table and automatically get a User record if the Entity we fetched is a user we have to do set the subclasses option in the parent class. The adjusted example:

<code type='php'>
lsmith's avatar
lsmith committed
37
class Entity extends Doctrine_Record {
hansbrix's avatar
hansbrix committed
38 39 40 41 42
    public function setTableDefinition() {
        $this->hasColumn('name','string',30);
        $this->hasColumn('username','string',20);
        $this->hasColumn('password','string',16);
        $this->hasColumn('created','integer',11);
lsmith's avatar
lsmith committed
43 44

        // this column is used for column
hansbrix's avatar
hansbrix committed
45 46
        // aggregation inheritance
        $this->hasColumn('type', 'integer', 11);
lsmith's avatar
lsmith committed
47
        $this->option('subclasses', array('User', 'Group'));
hansbrix's avatar
hansbrix committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    }
}

class User extends Entity {
    public function setUp() {
        $this->setInheritanceMap(array('type'=>1));
    }
}

class Group extends Entity {
    public function setUp() {
        $this->setInheritanceMap(array('type'=>2));
    }
}
</code>

We can then do the following given the previous table mapping.

<code type='php'>
$user = new User();
$user->name='Bjarte S. Karlsen';
$user->username='meus';
$user->password='rat';
$user->save();

$group = new Group();
$group->name='Users';
$group->username='users';
$group->password='password';
$group->save();

lsmith's avatar
lsmith committed
79
$q = new Doctrine_Query();
hansbrix's avatar
hansbrix committed
80 81
$user = $q->from('Entity')->where('id=?')->execute(array($user->id))->getFirst();

lsmith's avatar
lsmith committed
82
$q = new Doctrine_Query();
hansbrix's avatar
hansbrix committed
83 84 85 86
$group = $q->from('Entity')->where('id=?')->execute(array($group->id))->getFirst();
</code>

The user object is here an instance of User while the group object is an instance of Group.