1
0
mirror of synced 2024-12-14 15:16:04 +03:00

Several changes and fixes to the docs.

This commit is contained in:
Benjamin Eberlei 2010-12-03 17:58:01 +01:00
parent 1b9e9c019d
commit 6789f2c8d1
4 changed files with 48 additions and 36 deletions

View File

@ -374,9 +374,8 @@ steps and then discuss them step by step:
[php]
// Setup Autoloader (1)
require '/path/to/lib/Doctrine/Common/ClassLoader.php';
$loader = new Doctrine\Common\ClassLoader("Doctrine", '/path/to/Doctrine/trunk/lib/');
$loader->register();
// See the ORM Documentation Chapter "Configuration" for the up to day information
// on autoloading Doctrine 2.
$config = new Doctrine\ORM\Configuration(); // (2)

View File

@ -186,16 +186,6 @@ Here is an example:
// Register my type
Type::addType('mytype', 'My\Project\Types\MyType');
As can be seen above, when registering the custom types in the configuration you specify a unique name
for the mapping type and map that to the corresponding fully qualified class name. Now you can use your new type in your mapping like this:
[php]
class MyPersistentClass
{
/** @Column(type="mytype") */
private $field;
}
To have Schema-Tool convert the underlying database type of your new "mytype" directly into an instance of `MyType`
you have to additionally register this mapping with your database platform:
@ -207,6 +197,16 @@ Now using Schema-Tool, whenever it detects a column having the `db_mytype` it wi
Doctrine Type instance for Schema representation. Keep in mind that you can easily produce clashes this way,
each database type can only map to exactly one Doctrine mapping type.
As can be seen above, when registering the custom types in the configuration you specify a unique name
for the mapping type and map that to the corresponding fully qualified class name. Now you can use your new type in your mapping like this:
[php]
class MyPersistentClass
{
/** @Column(type="mytype") */
private $field;
}
++ Identifiers / Primary Keys
Every entity class needs an identifier/primary key. You designate the field that serves as the identifier with the `@Id` marker annotation. Here is an example:

View File

@ -50,8 +50,6 @@ The DDL for the corresponding database schema would look something like this (th
As you can see from this DDL snippet, there is only a single table for the entity subclass. All the mappings from the mapped superclass were inherited to the subclass as if they had been defined on that class directly.
++ Single Table Inheritance
[Single Table Inheritance](http://martinfowler.com/eaaCatalog/singleTableInheritance.html) is an inheritance mapping strategy where all classes of a hierarchy are mapped to a single database table. In order to distinguish which row represents which type in the hierarchy a so-called discriminator column is used.
@ -59,15 +57,15 @@ As you can see from this DDL snippet, there is only a single table for the entit
Example:
[php]
namespace MyProject\Model;
namespace Zoo\Entities;
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
* @DiscriminatorMap({"animal" = "Animal", "cat" = "Cat", "dog" = "Dog"})
*/
class Person
class Animal
{
// ...
}
@ -75,15 +73,23 @@ Example:
/**
* @Entity
*/
class Employee extends Person
class Cat extends Animal
{
// ...
}
/**
* @Entity
*/
class Dog extends Animal
{
// ...
}
Things to note:
* The @InheritanceType, @DiscriminatorColumn and @DiscriminatorMap must be specified on the topmost class that is part of the mapped entity hierarchy.
* The @DiscriminatorMap specifies which values of the discriminator column identify a row as being of a certain type. In the case above a value of "person" identifies a row as being of type `Person` and "employee" identifies a row as being of type `Employee`.
* The @InheritanceType, @DiscriminatorColumn and @DiscriminatorMap must be specified *ONLY* on the topmost class that is part of the mapped entity hierarchy.
* The @DiscriminatorMap specifies which values of the discriminator column identify a row as being of a certain type. In the case above a value of "animal" identifies a row as being of type `Animal`, "cat" identifies a row as being of type `Cat` and "dog" is an instance of `Dog`.
* The names of the classes in the discriminator map do not need to be fully qualified if the classes are contained in the same namespace as the entity class on which the discriminator map is applied.
+++ Design-time considerations
@ -111,29 +117,23 @@ Doctrine 2 implements this strategy through the use of a discriminator column in
Example:
[php]
namespace MyProject\Model;
namespace Zoo\Entities;
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
* @DiscriminatorMap({"animal" = "Animal", "cat" = "Cat", "dog" = "Dog"})
*/
class Person
{
// ...
}
/** @Entity */
class Employee extends Person
class Animal
{
// ...
}
Things to note:
* The @InheritanceType, @DiscriminatorColumn and @DiscriminatorMap must be specified on the topmost class that is part of the mapped entity hierarchy.
* The @DiscriminatorMap specifies which values of the discriminator column identify a row as being of which type. In the case above a value of "person" identifies a row as being of type `Person` and "employee" identifies a row as being of type `Employee`.
* The @InheritanceType, @DiscriminatorColumn and @DiscriminatorMap must be specified *ONLY* on the topmost class that is part of the mapped entity hierarchy.
* The @DiscriminatorMap specifies which values of the discriminator column identify a row as being of a certain type. In the case above a value of "animal" identifies a row as being of type `Animal`, "cat" identifies a row as being of type `Cat` and "dog" is an instance of `Dog`.
* The names of the classes in the discriminator map do not need to be fully qualified if the classes are contained in the same namespace as the entity class on which the discriminator map is applied.
> **NOTE**
@ -157,3 +157,16 @@ For each entity in the Class-Table Inheritance hierarchy all the mapped fields h
table of this entity. Additionally each child table has to have an id column that matches the id column
definition on the root table (except for any sequence or auto-increment details). Furthermore each child table has
to have a foreign key pointing from the id column to the root table id column and cascading on delete.
++ Querying Inheritance Hierachies
By definition you cannot query for the type of a MappedSuperclass. However in Single- or Class-Table Inheritance scenarios
you can query for any instance specified in the DiscriminatorMap or the root entity name. For both inheritance scenarios
the following queries would work:
[php]
$animals = $em->getRepository('Zoo\Entities\Animal')->findAll();
$cats = $em->getRepository('Zoo\Entities\Cat')->findAll();
$dogs = $em->getRepository('Zoo\Entities\Dog')->findAll();
This works for all finder methods, the Query Builder and DQL.

View File

@ -25,7 +25,7 @@ All the commands of the Doctrine Console require either the `db` or the `em` hel
In case of a project that is dealing exclusively with DBAL, the ConnectionHelper is required:
[php]
$helperSet = new \Symfony\Components\Console\Helper\HelperSet(array(
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($conn)
));
$cli->setHelperSet($helperSet);
@ -33,7 +33,7 @@ In case of a project that is dealing exclusively with DBAL, the ConnectionHelper
When dealing with the ORM package, the EntityManagerHelper is required:
[php]
$helperSet = new \Symfony\Components\Console\Helper\HelperSet(array(
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
$cli->setHelperSet($helperSet);
@ -62,7 +62,7 @@ bootstrap code and predefines the needed HelperSet attributes mentioned above. A
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$helperSet = new \Symfony\Components\Console\Helper\HelperSet(array(
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));