fixed docs related to seSubclasses/setInheritanceMap
This commit is contained in:
parent
37198a6bb7
commit
db6db4cd29
@ -342,7 +342,8 @@ In the following example we have one database table called {{entity}}. Users and
|
|||||||
|
|
||||||
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 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.
|
The only thing we have to do is to create 3 records (the same as before) and add
|
||||||
|
call the {{Doctrine_Table::setSubclasses()}} method from the parent class.
|
||||||
|
|
||||||
<code type="php">
|
<code type="php">
|
||||||
class Entity extends Doctrine_Record
|
class Entity extends Doctrine_Record
|
||||||
@ -357,63 +358,19 @@ class Entity extends Doctrine_Record
|
|||||||
// this column is used for column
|
// this column is used for column
|
||||||
// aggregation inheritance
|
// aggregation inheritance
|
||||||
$this->hasColumn('type', 'integer', 11);
|
$this->hasColumn('type', 'integer', 11);
|
||||||
|
$this->setSubclasses("User" => array("type => 1"), "Group" =>
|
||||||
|
array("type" => 2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class User extends Entity
|
class User extends Entity {}
|
||||||
{
|
class Group extends Entity {}
|
||||||
public function setUp()
|
|
||||||
{
|
|
||||||
$this->setInheritanceMap(array('type'=>1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Group extends Entity
|
|
||||||
{
|
|
||||||
public function setUp()
|
|
||||||
{
|
|
||||||
$this->setInheritanceMap(array('type'=>2));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</code>
|
</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:
|
This feature also enable us to query the {{Entity}} table and get a {{User}} or
|
||||||
|
{{Group}} object back if the returned object matches the constraints set in the
|
||||||
<code type="php">
|
parent class. See the code example below for an example of this.
|
||||||
class Entity extends Doctrine_Record
|
|
||||||
{
|
|
||||||
public function setTableDefinition()
|
|
||||||
{
|
|
||||||
$this->hasColumn('name', 'string', 30);
|
|
||||||
$this->hasColumn('username', 'string', 20);
|
|
||||||
$this->hasColumn('password', 'string', 16);
|
|
||||||
$this->hasColumn('created', 'integer', 11);
|
|
||||||
|
|
||||||
// this column is used for column
|
|
||||||
// aggregation inheritance
|
|
||||||
$this->hasColumn('type', 'integer', 11);
|
|
||||||
$this->option('subclasses', array('User', 'Group'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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">
|
<code type="php">
|
||||||
$user = new User();
|
$user = new User();
|
||||||
@ -430,13 +387,13 @@ $group->save();
|
|||||||
|
|
||||||
$q = new Doctrine_Query();
|
$q = new Doctrine_Query();
|
||||||
$user = $q->from('Entity')->where('id=?')->execute(array($user->id))->getFirst();
|
$user = $q->from('Entity')->where('id=?')->execute(array($user->id))->getFirst();
|
||||||
|
assert($user instanceOf User);
|
||||||
|
|
||||||
$q = new Doctrine_Query();
|
$q = new Doctrine_Query();
|
||||||
$group = $q->from('Entity')->where('id=?')->execute(array($group->id))->getFirst();
|
$group = $q->from('Entity')->where('id=?')->execute(array($group->id))->getFirst();
|
||||||
|
assert($group instanceOf Group);
|
||||||
</code>
|
</code>
|
||||||
|
|
||||||
The user object is here an instance of {{User}} while the group object is an instance of {{Group}}.
|
|
||||||
|
|
||||||
++ Foreign key constraints
|
++ Foreign key constraints
|
||||||
+++ Introduction
|
+++ Introduction
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user