Basic Mapping ============= 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: They all give exactly the same performance. 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. .. code-block:: php `_ or by the php.ini configuration ``date.timezone``. Working with different timezones will cause troubles and unexpected behavior. If you need specific timezone handling you have to handle this in your domain, converting all the values back and forth from UTC. There is also a `cookbook entry <../cookbook/working-with-datetime>` on working with datetimes that gives hints for implementing multi timezone applications. Property Mapping ---------------- 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. Example: .. code-block:: php getConnection(); $conn->getDatabasePlatform()->registerDoctrineTypeMapping('db_mytype', 'mytype'); Now using Schema-Tool, whenever it detects a column having the ``db_mytype`` it will convert it into a ``mytype`` 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. Custom ColumnDefinition ----------------------- You can define a custom definition for each column using the "columnDefinition" attribute of ``@Column``. You have to define all the definitions that follow the name of a column here. .. note:: Using columnDefinition will break change-detection in SchemaTool. 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: .. code-block:: php