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 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