This chapter explains the basic mapping of objects and properties. Mapping of associations will be covered in the next chapter "Association Mapping".
++ Mapping Drivers
Doctrine provides several different ways for specifying object-relational mapping metadata:
* Docblock Annotations
* XML
* YAML
This manual usually uses docblock annotations in all the examples that are spread throughout all chapters. There are dedicated chapters for XML and YAML mapping, respectively.
> **NOTE**
> If you're wondering which mapping driver gives the best performance, the answer is:
> None. Once the metadata of a class has been read from the source (annotations, xml or
> yaml) it is stored in an instance of the `Doctrine\ORM\Mapping\ClassMetadata` class
> and these instances are stored in the metadata cache. Therefore at the end of the day
> all drivers perform equally well. If you're not using a metadata cache (not
> recommended!) then the XML driver might have a slight edge in performance due to the
> powerful native XML support in PHP.
++ Introduction to Docblock Annotations
You've probably used docblock annotations in some form already, most likely to provide documentation metadata for a tool like `PHPDocumentor` (@author, @link, ...). Docblock annotations are a tool to embed metadata inside the documentation section which can then be processed by some tool. Doctrine 2 generalizes the concept of docblock annotations so that they can be used for any kind of metadata and so that it is easy to define new docblock annotations. In order to allow more involved annotation values and to reduce the chances of clashes with other docblock annotations, the Doctrine 2 docblock annotations feature an alternative syntax that is heavily inspired by the Annotation syntax introduced in Java 5.
The implementation of these enhanced docblock annotations is located in the `Doctrine\Common\Annotations` namespace and therefore part of the Common package. Doctrine 2 docblock annotations support namespaces and nested annotations among other things. The Doctrine 2 ORM defines its own set of docblock annotations for supplying object-relational mapping metadata.
> **NOTE**
> If you're not comfortable with the concept of docblock annotations, don't worry, as
> mentioned earlier Doctrine 2 provides XML and YAML alternatives and you could easily
> implement your own favourite mechanism for defining ORM metadata.
++ Persistent classes
In order to mark a class for object-relational persistence it needs to be designated as an entity. This can be done through the `@Entity` marker annotation.
By default, the entity will be persisted to a table with the same name as the class name. In order to change that, you can use the `@Table` annotation as follows:
A Doctrine Mapping Type defines the mapping between a PHP type and an SQL type. All Doctrine Mapping Types that ship with Doctrine are fully portable between different RDBMS. You can even write your own custom mapping types that might or might not be portable, which is explained later in this chapter.
For example, the Doctrine Mapping Type `string` defines the mapping from a PHP string to an SQL VARCHAR (or VARCHAR2 etc. depending on the RDBMS brand). Here is a quick overview of the built-in mapping types:
* `string`: Type that maps an SQL VARCHAR to a PHP string.
* `integer`: Type that maps an SQL INT to a PHP integer.
* `smallint`: Type that maps a database SMALLINT to a PHP integer.
* `bigint`: Type that maps a database BIGINT to a PHP string.
* `boolean`: Type that maps an SQL boolean to a PHP boolean.
* `decimal`: Type that maps an SQL DECIMAL to a PHP double.
* `date`: Type that maps an SQL DATETIME to a PHP DateTime object.
* `time`: Type that maps an SQL TIME to a PHP DateTime object.
* `datetime`: Type that maps an SQL DATETIME/TIMESTAMP to a PHP DateTime object.
* `text`: Type that maps an SQL CLOB to a PHP string.
After a class has been marked as an entity it can specify mappings for its instance fields. Here we will only look at simple fields that hold scalar values like strings, numbers, etc. Associations to other objects are covered in the chapter "Association Mapping".
To mark a property for relational persistence the `@Column` docblock annotation is used. This annotation usually requires at least 1 attribute to be set, the `type`. The `type` attribute specifies the Doctrine Mapping Type to use for the field. If the type is not specified, 'string' is used as the default mapping type since it is the most flexible.
In that example we mapped the field `id` to the column `id` using the mapping type `integer` and the field `name` is mapped to the column `name` with the default mapping type `string`. As you can see, by default the column names are assumed to be the same as the field names. To specify a different name for the column, you can use the `name` attribute of the Column annotation as follows:
[php]
/** @Column(name="db_name") */
private $name;
The Column annotation has some more attributes. Here is a complete list:
* `type`: (optional, defaults to 'string') The mapping type to use for the column.
* `name`: (optional, defaults to field name) The name of the column in the database.
* `length`: (optional, default 255) The length of the column in the database. (Applies only if a string-valued column is used).
* `unique`: (optional, default FALSE) Whether the column is a unique key.
* `nullable`: (optional, default FALSE) Whether the database column is nullable.
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:
[php]
class MyPersistentClass
{
/** @Id @Column(type="integer") */
private $id;
//...
}
Without doing anything else, the identifier is assumed to be manually assigned. That means your code would need to properly set the identifier property before passing a new entity to `EntityManager#persist($entity)`.
A common alternative strategy is to use a generated value as the identifier. To do this, you use the `@GeneratedValue` annotation like this:
This tells Doctrine to automatically generate a value for the identifier. How this value is generated is specified by the `strategy` attribute, which is optional and defaults to 'AUTO'. A value of `AUTO` tells Doctrine to use the generation strategy that is preferred by the currently used database platform. See below for details.
The previous example showed how to use the default identifier generation strategy without knowing the underlying database with the AUTO-detection strategy.
It is also possible to specifiy the identifier generation strategy more explicitly, which allows to make use of some additional features.
* `SEQUENCE`: Tells Doctrine to use a database sequence for ID generation. This strategy does currently not provide full portability. Sequences are supported by Oracle and PostgreSql.
* `IDENTITY`: Tells Doctrine to use special identity columns in the database that generate a value on insertion of a row. This strategy does currently not provide full portability and
is supported by the following platforms: MySQL/SQLite (AUTO_INCREMENT), MSSQL (IDENTITY) and PostgreSQL (SERIAL).
* `TABLE`: Tells Doctrine to use a separate table for ID generation. This strategy provides full portability. ***This strategy is not yet implemented!***
Doctrine 2 allows to use composite primary keys. There are however some restrictions oposed to using a single identifier.
The use of the `@GeneratedValue` annotation is only supported for simple (not composite) primary keys, which means
you can only use composite keys if you generate the primary key values yourself before calling `EntityManager#persist()`
on the entity.
To designate a composite primary key / identifier, simply put the @Id marker annotation on all fields that make up the primary key.
++ Quoting Reserved Words
It may sometimes be necessary to quote a column or table name because it conflicts with a reserved word of the particular RDBMS in use. This is often referred to as "Identifier Quoting". To let Doctrine know that you would like a table or column name to be quoted in all SQL statements, enclose the table or column name in backticks. Here is an example:
[php]
/** @Column(name="`number`", type="integer") */
private $number;
Doctrine will then quote this column name in all SQL statements according to the used database platform.
> **CAUTION**
> Identifier Quoting is not supported for join column names or discriminator column names.
> **CAUTION**
> Identifier Quoting is a feature that is mainly intended to support legacy database