Annotations Reference ===================== 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. In this chapter a reference of every Doctrine 2 Annotation is given with short explanations on their context and usage. Index ----- - :ref:`@Column ` - :ref:`@ColumnResult ` - :ref:`@ChangeTrackingPolicy ` - :ref:`@DiscriminatorColumn ` - :ref:`@DiscriminatorMap ` - :ref:`@Entity ` - :ref:`@EntityResult ` - :ref:`@FieldResult ` - :ref:`@GeneratedValue ` - :ref:`@HasLifecycleCallbacks ` - :ref:`@Index ` - :ref:`@Id ` - :ref:`@InheritanceType ` - :ref:`@JoinColumn ` - :ref:`@JoinColumns ` - :ref:`@JoinTable ` - :ref:`@ManyToOne ` - :ref:`@ManyToMany ` - :ref:`@MappedSuperclass ` - :ref:`@NamedNativeQuery ` - :ref:`@OneToOne ` - :ref:`@OneToMany ` - :ref:`@OrderBy ` - :ref:`@PostLoad ` - :ref:`@PostPersist ` - :ref:`@PostRemove ` - :ref:`@PostUpdate ` - :ref:`@PrePersist ` - :ref:`@PreRemove ` - :ref:`@PreUpdate ` - :ref:`@SequenceGenerator ` - :ref:`@SqlResultSetMapping ` - :ref:`@Table ` - :ref:`@UniqueConstraint ` - :ref:`@Version ` Reference --------- .. _annref_column: @Column ~~~~~~~ Marks an annotated instance variable as "persistent". It has to be inside the instance variables PHP DocBlock comment. Any value hold inside this variable will be saved to and loaded from the database as part of the lifecycle of the instance variables entity-class. Required attributes: - **type**: Name of the Doctrine Type which is converted between PHP and Database representation. Optional attributes: - **name**: By default the property name is used for the database column name also, however the 'name' attribute allows you to determine the column name. - **length**: Used by the "string" type to determine its maximum length in the database. Doctrine does not validate the length of a string values for you. - **precision**: The precision for a decimal (exact numeric) column (Applies only for decimal column) - **scale**: The scale for a decimal (exact numeric) column (Applies only for decimal column) - **unique**: Boolean value to determine if the value of the column should be unique across all rows of the underlying entities table. - **nullable**: Determines if NULL values allowed for this column. - **columnDefinition**: DDL SQL snippet that starts after the column name and specifies the complete (non-portable!) column definition. This attribute allows to make use of advanced RMDBS features. However you should make careful use of this feature and the consequences. SchemaTool will not detect changes on the column correctly anymore if you use "columnDefinition". Additionally you should remember that the "type" attribute still handles the conversion between PHP and Database values. If you use this attribute on a column that is used for joins between tables you should also take a look at :ref:`@JoinColumn `. Examples: .. code-block:: php ` can be found in the configuration section. Example: .. code-block:: php = 2.1) Specifies that this entity is marked as read only and not considered for change-tracking. Entities of this type can be persisted and removed though. Example: .. code-block:: php `. This annotation is optional and only has meaning when used in conjunction with @Id. If this annotation is not specified with @Id the NONE strategy is used as default. Required attributes: - **strategy**: Set the name of the identifier generation strategy. Valid values are AUTO, SEQUENCE, TABLE, IDENTITY, UUID, CUSTOM and NONE. Example: .. code-block:: php ` annotation on the entity-class level. It allows to hint the SchemaTool to generate a database index on the specified table columns. It only has meaning in the SchemaTool schema generation context. Required attributes: - **name**: Name of the Index - **columns**: Array of columns. Example: .. code-block:: php ` and :ref:`@DiscriminatorColumn ` annotations. Examples: .. code-block:: php `, :ref:`@OneToOne ` fields and in the Context of :ref:`@JoinTable ` nested inside a @ManyToMany. This annotation is not required. If its not specified the attributes *name* and *referencedColumnName* are inferred from the table and primary key names. Required attributes: - **name**: Column name that holds the foreign key identifier for this relation. In the context of @JoinTable it specifies the column name in the join table. - **referencedColumnName**: Name of the primary key identifier that is used for joining of this relation. Optional attributes: - **unique**: Determines if this relation exclusive between the affected entities and should be enforced so on the database constraint level. Defaults to false. - **nullable**: Determine if the related entity is required, or if null is an allowed state for the relation. Defaults to true. - **onDelete**: Cascade Action (Database-level) - **columnDefinition**: DDL SQL snippet that starts after the column name and specifies the complete (non-portable!) column definition. This attribute allows to make use of advanced RMDBS features. Using this attribute on @JoinColumn is necessary if you need slightly different column definitions for joining columns, for example regarding NULL/NOT NULL defaults. However by default a "columnDefinition" attribute on :ref:`@Column ` also sets the related @JoinColumn's columnDefinition. This is necessary to make foreign keys work. Example: .. code-block:: php ` or :ref:`@OneToOne ` relation with an entity that has multiple identifiers. .. _annref_jointable: @JoinTable ~~~~~~~~~~~~~~ Using :ref:`@OneToMany ` or :ref:`@ManyToMany ` on the owning side of the relation requires to specify the @JoinTable annotation which describes the details of the database join table. If you do not specify @JoinTable on these relations reasonable mapping defaults apply using the affected table and the column names. Required attributes: - **name**: Database name of the join-table - **joinColumns**: An array of @JoinColumn annotations describing the join-relation between the owning entities table and the join table. - **inverseJoinColumns**: An array of @JoinColumn annotations describing the join-relation between the inverse entities table and the join table. Example: .. code-block:: php ` is an additional, optional annotation that has reasonable default configuration values using the table and names of the two related entities. Required attributes: - **targetEntity**: FQCN of the referenced target entity. Can be the unqualified class name if both classes are in the same namespace. *IMPORTANT:* No leading backslash! Optional attributes: - **mappedBy**: This option specifies the property name on the targetEntity that is the owning side of this relation. Its a required attribute for the inverse side of a relationship. - **inversedBy**: The inversedBy attribute designates the field in the entity that is the inverse side of the relationship. - **cascade**: Cascade Option - **fetch**: One of LAZY, EXTRA_LAZY or EAGER - **indexBy**: Index the collection by a field on the target entity. .. note:: For ManyToMany bidirectional relationships either side may be the owning side (the side that defines the @JoinTable and/or does not make use of the mappedBy attribute, thus using a default join table). Example: .. code-block:: php `. Optional attributes: - **repositoryClass**: (>= 2.2) Specifies the FQCN of a subclass of the EntityRepository. That will be inherited for all subclasses of that Mapped Superclass. Example: .. code-block:: php ` with one additional option that can be specified. The configuration defaults for :ref:`@JoinColumn ` using the target entity table and primary key column names apply here too. Required attributes: - **targetEntity**: FQCN of the referenced target entity. Can be the unqualified class name if both classes are in the same namespace. *IMPORTANT:* No leading backslash! Optional attributes: - **cascade**: Cascade Option - **fetch**: One of LAZY or EAGER - **orphanRemoval**: Boolean that specifies if orphans, inverse OneToOne entities that are not connected to any owning instance, should be removed by Doctrine. Defaults to false. - **inversedBy**: The inversedBy attribute designates the field in the entity that is the inverse side of the relationship. Example: .. code-block:: php ` or :ref:`@OneToMany ` annotation to specify by which criteria the collection should be retrieved from the database by using an ORDER BY clause. This annotation requires a single non-attributed value with an DQL snippet: Example: .. code-block:: php ` annotation on the entity-class level. It allows to hint the SchemaTool to generate a database unique constraint on the specified table columns. It only has meaning in the SchemaTool schema generation context. Required attributes: - **name**: Name of the Index - **columns**: Array of columns. Example: .. code-block:: php ` annotations that have the type integer or datetime. Combining @Version with :ref:`@Id ` is not supported. Example: .. code-block:: php