2010-11-02 00:03:50 +03:00
Annotations Reference
=====================
2010-11-01 23:16:12 +03:00
In this chapter a reference of every Doctrine 2 Annotation is given
with short explanations on their context and usage.
Index
-----
2010-12-03 22:13:10 +03:00
- :ref: `@Column <annref_column>`
2012-04-15 23:57:44 +04:00
- :ref: `@ColumnResult <annref_column_result>`
2010-12-03 22:13:10 +03:00
- :ref: `@ChangeTrackingPolicy <annref_changetrackingpolicy>`
- :ref: `@DiscriminatorColumn <annref_discriminatorcolumn>`
- :ref: `@DiscriminatorMap <annref_discriminatormap>`
- :ref: `@Entity <annref_entity>`
2012-04-15 23:57:44 +04:00
- :ref: `@EntityResult <annref_entity_result>`
- :ref: `@FieldResult <annref_field_result>`
2010-12-03 22:13:10 +03:00
- :ref: `@GeneratedValue <annref_generatedvalue>`
- :ref: `@HasLifecycleCallbacks <annref_haslifecyclecallbacks>`
- :ref: `@Index <annref_index>`
- :ref: `@Id <annref_id>`
- :ref: `@InheritanceType <annref_inheritancetype>`
- :ref: `@JoinColumn <annref_joincolumn>`
- :ref: `@JoinTable <annref_jointable>`
- :ref: `@ManyToOne <annref_manytoone>`
- :ref: `@ManyToMany <annref_manytomany>`
- :ref: `@MappedSuperclass <annref_mappedsuperclass>`
2012-04-15 23:57:44 +04:00
- :ref: `@NamedNativeQuery <annref_named_native_query>`
2010-12-03 22:13:10 +03:00
- :ref: `@OneToOne <annref_onetoone>`
- :ref: `@OneToMany <annref_onetomany>`
- :ref: `@OrderBy <annref_orderby>`
- :ref: `@PostLoad <annref_postload>`
- :ref: `@PostPersist <annref_postpersist>`
- :ref: `@PostRemove <annref_postremove>`
- :ref: `@PostUpdate <annref_postupdate>`
- :ref: `@PrePersist <annref_prepersist>`
- :ref: `@PreRemove <annref_preremove>`
- :ref: `@PreUpdate <annref_preupdate>`
- :ref: `@SequenceGenerator <annref_sequencegenerator>`
2012-04-15 23:57:44 +04:00
- :ref: `@SqlResultSetMapping <annref_sql_resultset_mapping>`
2010-12-03 22:13:10 +03:00
- :ref: `@Table <annref_table>`
- :ref: `@UniqueConstraint <annref_uniqueconstraint>`
- :ref: `@Version <annref_version>`
2010-11-01 23:16:12 +03:00
Reference
---------
2010-12-03 22:13:10 +03:00
.. _annref_column:
2010-11-02 00:03:50 +03:00
@Column
~~~~~~~
2010-11-01 23:16:12 +03:00
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:
2011-01-02 13:53:12 +03:00
- **type** : Name of the Doctrine Type which is converted between PHP
2010-11-01 23:16:12 +03:00
and Database representation.
Optional attributes:
2011-01-02 13:53:12 +03:00
- **name** : By default the property name is used for the database
2010-11-01 23:16:12 +03:00
column name also, however the 'name' attribute allows you to
determine the column name.
2011-01-02 13:53:12 +03:00
- **length** : Used by the "string" type to determine its maximum
2010-11-01 23:16:12 +03:00
length in the database. Doctrine does not validate the length of a
string values for you.
2011-01-02 13:53:12 +03:00
- **precision** : The precision for a decimal (exact numeric) column
2010-11-01 23:16:12 +03:00
(Applies only for decimal column)
2011-01-02 13:53:12 +03:00
- **scale** : The scale for a decimal (exact numeric) column (Applies
2010-11-01 23:16:12 +03:00
only for decimal column)
2011-01-02 13:53:12 +03:00
- **unique** : Boolean value to determine if the value of the column
2010-11-01 23:16:12 +03:00
should be unique across all rows of the underlying entities table.
2011-01-02 13:53:12 +03:00
- **nullable** : Determines if NULL values allowed for this column.
- **columnDefinition** : DDL SQL snippet that starts after the column
2010-11-01 23:16:12 +03:00
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
2011-02-15 23:31:20 +03:00
consequences. SchemaTool will not detect changes on the column correctly
anymore if you use "columnDefinition".
Additionally you should remember that the "type"
2010-11-01 23:16:12 +03:00
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
2011-02-15 23:31:20 +03:00
:ref: `@JoinColumn <annref_joincolumn>` .
2010-11-01 23:16:12 +03:00
Examples:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Column(type="string", length=32, unique=true, nullable=false)
*/
protected $username;
/**
* @Column(type="string", columnDefinition="CHAR(2) NOT NULL")
*/
protected $country;
/**
* @Column(type="decimal", precision=2, scale=1)
*/
protected $height;
2012-04-15 23:57:44 +04:00
.. _annref_column_result:
@ColumnResult
~~~~~~~~~~~~~~
References name of a column in the SELECT clause of a SQL query.
Scalar result types can be included in the query result by specifying this annotation in the metadata.
Required attributes:
- **name** : The name of a column in the SELECT clause of a SQL query
2010-12-03 22:13:10 +03:00
.. _annref_changetrackingpolicy:
2010-11-02 00:03:50 +03:00
@ChangeTrackingPolicy
~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
The Change Tracking Policy annotation allows to specify how the
Doctrine 2 UnitOfWork should detect changes in properties of
entities during flush. By default each entity is checked according
to a deferred implicit strategy, which means upon flush UnitOfWork
compares all the properties of an entity to a previously stored
snapshot. This works out of the box, however you might want to
tweak the flush performance where using another change tracking
policy is an interesting option.
2010-12-03 22:13:10 +03:00
The :doc: `details on all the available change tracking policies <change-tracking-policies>`
2010-11-01 23:16:12 +03:00
can be found in the configuration section.
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Entity
* @ChangeTrackingPolicy("DEFERRED_IMPLICIT")
* @ChangeTrackingPolicy("DEFERRED_EXPLICIT")
* @ChangeTrackingPolicy("NOTIFY")
*/
class User {}
2010-12-03 22:13:10 +03:00
.. _annref_discriminatorcolumn:
2012-04-16 19:15:18 +04:00
@DiscriminatorColumn
2010-11-02 00:03:50 +03:00
~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
This annotation is a required annotation for the topmost/super
class of an inheritance hierarchy. It specifies the details of the
column which saves the name of the class, which the entity is
actually instantiated as.
Required attributes:
2011-01-02 13:53:12 +03:00
- **name** : The column name of the discriminator. This name is also
2010-11-01 23:16:12 +03:00
used during Array hydration as key to specify the class-name.
Optional attributes:
2011-01-02 13:53:12 +03:00
- **type** : By default this is string.
- **length** : By default this is 255.
2010-11-01 23:16:12 +03:00
2010-12-03 22:13:10 +03:00
.. _annref_discriminatormap:
2010-11-02 00:03:50 +03:00
@DiscriminatorMap
~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
The discriminator map is a required annotation on the
top-most/super class in an inheritance hierarchy. It takes an array
as only argument which defines which class should be saved under
which name in the database. Keys are the database value and values
are the classes, either as fully- or as unqualified class names
depending if the classes are in the namespace or not.
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
2010-12-03 22:13:10 +03:00
.. _annref_entity:
2010-11-02 00:03:50 +03:00
@Entity
~~~~~~~
2010-11-01 23:16:12 +03:00
Required annotation to mark a PHP class as Entity. Doctrine manages
the persistence of all classes marked as entity.
Optional attributes:
2011-01-02 13:53:12 +03:00
- **repositoryClass** : Specifies the FQCN of a subclass of the
2011-12-20 00:35:01 +04:00
EntityRepository. Use of repositories for entities is encouraged to keep
2010-11-01 23:16:12 +03:00
specialized DQL and SQL operations separated from the Model/Domain
Layer.
2011-03-29 22:30:10 +04:00
- **readOnly** : (>= 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.
2010-11-01 23:16:12 +03:00
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Entity(repositoryClass="MyProject\UserRepository")
*/
class User
{
//...
}
2012-04-15 23:57:44 +04:00
.. _annref_entity_result:
@EntityResult
~~~~~~~~~~~~~~
References an entity in the SELECT clause of a SQL query.
If this annotation is used, the SQL statement should select all of the columns that are mapped to the entity object.
This should include foreign key columns to related entities.
The results obtained when insufficient data is available are undefined.
Required attributes:
- **entityClass** : The class of the result.
Optional attributes:
- **fields** : Array of @FieldResult, Maps the columns specified in the SELECT list of the query to the properties or fields of the entity class.
- **discriminatorColumn** : Specifies the column name of the column in the SELECT list that is used to determine the type of the entity instance.
.. _annref_field_result:
@FieldResult
~~~~~~~~~~~~~
Is used to map the columns specified in the SELECT list of the query to the properties or fields of the entity class.
Required attributes:
- **name** : Name of the persistent field or property of the class.
Optional attributes:
- **column** : Name of the column in the SELECT clause.
2010-12-03 22:13:10 +03:00
.. _annref_generatedvalue:
2010-11-02 00:03:50 +03:00
@GeneratedValue
~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Specifies which strategy is used for identifier generation for an
2010-12-03 22:13:10 +03:00
instance variable which is annotated by :ref: `@Id <annref_id>` . This
2010-11-01 23:16:12 +03:00
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:
2011-01-02 13:53:12 +03:00
- **strategy** : Set the name of the identifier generation strategy.
2012-10-18 19:34:22 +04:00
Valid values are AUTO, SEQUENCE, TABLE, IDENTITY, UUID, CUSTOM and NONE.
2010-11-01 23:16:12 +03:00
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Id
* @Column(type="integer")
2012-09-18 12:28:24 +04:00
* @GeneratedValue(strategy="IDENTITY")
2010-11-01 23:16:12 +03:00
*/
protected $id = null;
2010-12-03 22:13:10 +03:00
.. _annref_haslifecyclecallbacks:
2010-11-02 00:03:50 +03:00
@HasLifecycleCallbacks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Annotation which has to be set on the entity-class PHP DocBlock to
notify Doctrine that this entity has entity life-cycle callback
annotations set on at least one of its methods. Using @PostLoad,
@PrePersist, @PostPersist, @PreRemove, @PostRemove, @PreUpdate or
@PostUpdate without this marker annotation will make Doctrine
ignore the callbacks.
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Entity
* @HasLifecycleCallbacks
*/
class User
{
/**
* @PostPersist
*/
public function sendOptinMail() {}
}
2010-12-03 22:13:10 +03:00
.. _annref_index:
2010-11-02 00:03:50 +03:00
@Index
~~~~~~~
2010-11-01 23:16:12 +03:00
2010-12-03 22:13:10 +03:00
Annotation is used inside the :ref: `@Table <annref_table>` annotation on
2010-11-01 23:16:12 +03:00
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:
2011-01-02 13:53:12 +03:00
- **name** : Name of the Index
- **columns** : Array of columns.
2010-11-01 23:16:12 +03:00
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Entity
2012-11-16 14:21:06 +04:00
* @Table(name="ecommerce_products",indexes={@Index(name="search_idx", columns={"name", "email"})})
2010-11-01 23:16:12 +03:00
*/
class ECommerceProduct
{
}
2010-12-03 22:13:10 +03:00
.. _annref_id:
2010-11-02 00:03:50 +03:00
@Id
~~~~~~~
2010-11-01 23:16:12 +03:00
The annotated instance variable will be marked as entity
identifier, the primary key in the database. This annotation is a
marker only and has no required or optional attributes. For
entities that have multiple identifier columns each column has to
be marked with @Id.
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Id
* @Column(type="integer")
*/
protected $id = null;
2010-12-03 22:13:10 +03:00
.. _annref_inheritancetype:
2010-11-02 00:03:50 +03:00
@InheritanceType
~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
In an inheritance hierarchy you have to use this annotation on the
topmost/super class to define which strategy should be used for
inheritance. Currently Single Table and Class Table Inheritance are
supported.
This annotation has always been used in conjunction with the
2010-12-03 22:13:10 +03:00
:ref: `@DiscriminatorMap <annref_discriminatormap>` and
:ref: `@DiscriminatorColumn <annref_discriminatorcolumn>` annotations.
2010-11-01 23:16:12 +03:00
Examples:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
/**
* @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
*/
class Person
{
// ...
}
2010-12-03 22:13:10 +03:00
.. _annref_joincolumn:
2010-11-02 00:03:50 +03:00
@JoinColumn
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
This annotation is used in the context of relations in
2010-12-03 22:13:10 +03:00
:ref: `@ManyToOne <annref_manytoone>` , :ref: `@OneToOne <annref_onetoone>` fields
and in the Context of :ref: `@JoinTable <annref_jointable>` nested inside
2010-11-01 23:16:12 +03:00
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:
2011-01-02 13:53:12 +03:00
- **name** : Column name that holds the foreign key identifier for
2010-11-01 23:16:12 +03:00
this relation. In the context of @JoinTable it specifies the column
name in the join table.
2011-01-02 13:53:12 +03:00
- **referencedColumnName** : Name of the primary key identifier that
2010-11-01 23:16:12 +03:00
is used for joining of this relation.
Optional attributes:
2011-01-02 13:53:12 +03:00
- **unique** : Determines if this relation exclusive between the
2010-11-01 23:16:12 +03:00
affected entities and should be enforced so on the database
constraint level. Defaults to false.
2011-01-02 13:53:12 +03:00
- **nullable** : Determine if the related entity is required, or if
2010-11-01 23:16:12 +03:00
null is an allowed state for the relation. Defaults to true.
2011-01-02 13:53:12 +03:00
- **onDelete** : Cascade Action (Database-level)
- **columnDefinition** : DDL SQL snippet that starts after the column
2010-11-01 23:16:12 +03:00
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
2010-12-03 22:13:10 +03:00
"columnDefinition" attribute on :ref: `@Column <annref_column>` also sets
2010-11-01 23:16:12 +03:00
the related @JoinColumn's columnDefinition. This is necessary to
make foreign keys work.
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @OneToOne(targetEntity="Customer")
* @JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
2010-12-03 22:13:10 +03:00
.. _annref_joincolumns:
2010-11-02 00:03:50 +03:00
@JoinColumns
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
An array of @JoinColumn annotations for a
2010-12-03 22:13:10 +03:00
:ref: `@ManyToOne <annref_manytoone>` or :ref: `@OneToOne <annref_onetoone>`
2010-11-01 23:16:12 +03:00
relation with an entity that has multiple identifiers.
2010-12-03 22:13:10 +03:00
.. _annref_jointable:
2010-11-02 00:03:50 +03:00
@JoinTable
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
2010-12-03 22:13:10 +03:00
Using :ref: `@OneToMany <annref_onetomany>` or
:ref: `@ManyToMany <annref_manytomany>` on the owning side of the relation
2010-11-01 23:16:12 +03:00
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:
2011-01-02 13:53:12 +03:00
- **name** : Database name of the join-table
- **joinColumns** : An array of @JoinColumn annotations describing the
2010-11-01 23:16:12 +03:00
join-relation between the owning entities table and the join table.
2011-01-02 13:53:12 +03:00
- **inverseJoinColumns** : An array of @JoinColumn annotations
2010-11-01 23:16:12 +03:00
describing the join-relation between the inverse entities table and
the join table.
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @ManyToMany(targetEntity="Phonenumber")
* @JoinTable(name="users_phonenumbers",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="phonenumber_id", referencedColumnName="id", unique=true)}
* )
*/
public $phonenumbers;
2010-12-03 22:13:10 +03:00
.. _annref_manytoone:
2010-11-02 00:03:50 +03:00
@ManyToOne
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Defines that the annotated instance variable holds a reference that
describes a many-to-one relationship between two entities.
Required attributes:
2011-01-02 13:53:12 +03:00
- **targetEntity** : FQCN of the referenced target entity. Can be the
2010-11-01 23:16:12 +03:00
unqualified class name if both classes are in the same namespace.
*IMPORTANT:* No leading backslash!
Optional attributes:
2011-01-02 13:53:12 +03:00
- **cascade** : Cascade Option
- **fetch** : One of LAZY or EAGER
2010-11-01 23:16:12 +03:00
- inversedBy - The inversedBy attribute designates the field in
the entity that is the inverse side of the relationship.
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
2011-03-28 22:40:39 +04:00
* @ManyToOne(targetEntity="Cart", cascade={"all"}, fetch="EAGER")
2010-11-01 23:16:12 +03:00
*/
private $cart;
2010-12-03 22:13:10 +03:00
.. _annref_manytomany:
2010-11-02 00:03:50 +03:00
@ManyToMany
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Defines an instance variable holds a many-to-many relationship
2010-12-03 22:13:10 +03:00
between two entities. :ref: `@JoinTable <annref_jointable>` is an
2010-11-01 23:16:12 +03:00
additional, optional annotation that has reasonable default
configuration values using the table and names of the two related
entities.
Required attributes:
2011-01-02 13:53:12 +03:00
- **targetEntity** : FQCN of the referenced target entity. Can be the
2010-11-01 23:16:12 +03:00
unqualified class name if both classes are in the same namespace.
*IMPORTANT:* No leading backslash!
Optional attributes:
2011-01-02 13:53:12 +03:00
- **mappedBy** : This option specifies the property name on the
2010-11-01 23:16:12 +03:00
targetEntity that is the owning side of this relation. Its a
required attribute for the inverse side of a relationship.
2011-01-02 13:53:12 +03:00
- **inversedBy** : The inversedBy attribute designates the field in the
2010-11-01 23:16:12 +03:00
entity that is the inverse side of the relationship.
2011-01-02 13:53:12 +03:00
- **cascade** : Cascade Option
2011-07-13 22:31:01 +04:00
- **fetch** : One of LAZY, EXTRA_LAZY or EAGER
- **indexBy** : Index the collection by a field on the target entity.
.. note ::
2010-11-01 23:16:12 +03:00
2011-07-13 22:31:01 +04:00
For ManyToMany bidirectional relationships either side may
2010-11-01 23:16:12 +03:00
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:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* Owning Side
*
* @ManyToMany(targetEntity="Group", inversedBy="features")
* @JoinTable(name="user_groups",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
private $groups;
/**
* Inverse Side
*
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
private $features;
2010-12-03 22:13:10 +03:00
.. _annref_mappedsuperclass:
2010-11-02 00:03:50 +03:00
@MappedSuperclass
~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
An mapped superclass is an abstract or concrete class that provides
persistent entity state and mapping information for its subclasses,
but which is not itself an entity. This annotation is specified on
the Class docblock and has no additional attributes.
The @MappedSuperclass annotation cannot be used in conjunction with
@Entity. See the Inheritance Mapping section for
2010-12-03 22:13:10 +03:00
:doc: `more details on the restrictions of mapped superclasses <inheritance-mapping>` .
2011-12-20 00:35:01 +04:00
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
<?php
2012-09-18 12:28:24 +04:00
/**
* @MappedSuperclass
*/
2011-12-20 00:35:01 +04:00
class MappedSuperclassBase
{
// ... fields and methods
}
2012-09-18 12:28:24 +04:00
/**
* @Entity
*/
2011-12-20 00:35:01 +04:00
class EntitySubClassFoo extends MappedSuperclassBase
{
// ... fields and methods
}
2012-04-15 23:57:44 +04:00
.. _annref_named_native_query:
@NamedNativeQuery
~~~~~~~~~~~~~~~~~
Is used to specify a native SQL named query.
The NamedNativeQuery annotation can be applied to an entity or mapped superclass.
Required attributes:
- **name** : The name used to refer to the query with the EntityManager methods that create query objects.
- **query** : The SQL query string.
Optional attributes:
- **resultClass** : The class of the result.
2012-09-18 12:28:24 +04:00
- **resultSetMapping** : The name of a SqlResultSetMapping, as defined in metadata.
2012-04-15 23:57:44 +04:00
Example:
.. code-block :: php
<?php
/**
* @NamedNativeQueries({
* @NamedNativeQuery(
* name = "fetchJoinedAddress",
* resultSetMapping= "mappingJoinedAddress",
* query = "SELECT u.id, u.name, u.status, a.id AS a_id, a.country, a.zip, a.city FROM cms_users u INNER JOIN cms_addresses a ON u.id = a.user_id WHERE u.username = ?"
* ),
* })
* @SqlResultSetMappings({
* @SqlResultSetMapping(
* name = "mappingJoinedAddress",
* entities= {
* @EntityResult(
* entityClass = "__CLASS__",
* fields = {
* @FieldResult(name = "id"),
* @FieldResult(name = "name"),
* @FieldResult(name = "status"),
* @FieldResult(name = "address.zip"),
* @FieldResult(name = "address.city"),
* @FieldResult(name = "address.country"),
* @FieldResult(name = "address.id", column = "a_id"),
* }
* )
* }
* )
* })
*/
class User
{
/** @Id @Column(type="integer") @GeneratedValue * /
public $id;
/** @Column(type="string", length=50, nullable=true) * /
public $status;
/** @Column(type="string", length=255, unique=true) * /
public $username;
/** @Column(type="string", length=255) * /
public $name;
/** @OneToOne(targetEntity="Address") * /
public $address;
// ....
}
2010-12-03 22:13:10 +03:00
.. _annref_onetoone:
2010-11-01 23:16:12 +03:00
2010-11-02 00:03:50 +03:00
@OneToOne
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
The @OneToOne annotation works almost exactly as the
2010-12-03 22:13:10 +03:00
:ref: `@ManyToOne <annref_manytoone>` with one additional option that can
2010-11-01 23:16:12 +03:00
be specified. The configuration defaults for
2010-12-03 22:13:10 +03:00
:ref: `@JoinColumn <annref_joincolumn>` using the target entity table and
2010-11-01 23:16:12 +03:00
primary key column names apply here too.
Required attributes:
2011-01-02 13:53:12 +03:00
- **targetEntity** : FQCN of the referenced target entity. Can be the
2010-11-01 23:16:12 +03:00
unqualified class name if both classes are in the same namespace.
*IMPORTANT:* No leading backslash!
Optional attributes:
2011-01-02 13:53:12 +03:00
- **cascade** : Cascade Option
- **fetch** : One of LAZY or EAGER
- **orphanRemoval** : Boolean that specifies if orphans, inverse
2010-11-01 23:16:12 +03:00
OneToOne entities that are not connected to any owning instance,
should be removed by Doctrine. Defaults to false.
2011-01-02 13:53:12 +03:00
- **inversedBy** : The inversedBy attribute designates the field in the
2010-11-01 23:16:12 +03:00
entity that is the inverse side of the relationship.
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @OneToOne(targetEntity="Customer")
* @JoinColumn(name="customer_id", referencedColumnName="id")
*/
private $customer;
2010-12-03 22:13:10 +03:00
.. _annref_onetomany:
2010-11-02 00:03:50 +03:00
@OneToMany
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Required attributes:
2011-01-02 13:53:12 +03:00
- **targetEntity** : FQCN of the referenced target entity. Can be the
2010-11-01 23:16:12 +03:00
unqualified class name if both classes are in the same namespace.
*IMPORTANT:* No leading backslash!
Optional attributes:
2011-01-02 13:53:12 +03:00
- **cascade** : Cascade Option
- **orphanRemoval** : Boolean that specifies if orphans, inverse
2010-11-01 23:16:12 +03:00
OneToOne entities that are not connected to any owning instance,
should be removed by Doctrine. Defaults to false.
2011-01-02 13:53:12 +03:00
- **mappedBy** : This option specifies the property name on the
2010-11-01 23:16:12 +03:00
targetEntity that is the owning side of this relation. Its a
required attribute for the inverse side of a relationship.
2011-07-13 22:31:01 +04:00
- **fetch** : One of LAZY, EXTRA_LAZY or EAGER.
- **indexBy** : Index the collection by a field on the target entity.
2010-11-01 23:16:12 +03:00
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @OneToMany(targetEntity="Phonenumber", mappedBy="user", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
public $phonenumbers;
2010-12-03 22:13:10 +03:00
.. _annref_orderby:
2010-11-02 00:03:50 +03:00
@OrderBy
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Optional annotation that can be specified with a
2010-12-03 22:13:10 +03:00
:ref: `@ManyToMany <annref_manytomany>` or :ref: `@OneToMany <annref_onetomany>`
2010-11-01 23:16:12 +03:00
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:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @ManyToMany(targetEntity="Group")
* @OrderBy({"name" = "ASC"})
*/
private $groups;
The DQL Snippet in OrderBy is only allowed to consist of
unqualified, unquoted field names and of an optional ASC/DESC
positional statement. Multiple Fields are separated by a comma (,).
The referenced field names have to exist on the `` targetEntity ``
class of the `` @ManyToMany `` or `` @OneToMany `` annotation.
2010-12-03 22:13:10 +03:00
.. _annref_postload:
2010-11-02 00:03:50 +03:00
@PostLoad
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Marks a method on the entity to be called as a @PostLoad event.
Only works with @HasLifecycleCallbacks in the entity class PHP
DocBlock.
2010-12-03 22:13:10 +03:00
.. _annref_postpersist:
2010-11-02 00:03:50 +03:00
@PostPersist
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Marks a method on the entity to be called as a @PostPersist event.
Only works with @HasLifecycleCallbacks in the entity class PHP
DocBlock.
2010-12-03 22:13:10 +03:00
.. _annref_postremove:
2010-11-02 00:03:50 +03:00
@PostRemove
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Marks a method on the entity to be called as a @PostRemove event.
Only works with @HasLifecycleCallbacks in the entity class PHP
DocBlock.
2010-12-03 22:13:10 +03:00
.. _annref_postupdate:
2010-11-02 00:03:50 +03:00
@PostUpdate
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Marks a method on the entity to be called as a @PostUpdate event.
Only works with @HasLifecycleCallbacks in the entity class PHP
DocBlock.
2010-12-03 22:13:10 +03:00
.. _annref_prepersist:
2010-11-02 00:03:50 +03:00
@PrePersist
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Marks a method on the entity to be called as a @PrePersist event.
Only works with @HasLifecycleCallbacks in the entity class PHP
DocBlock.
2010-12-03 22:13:10 +03:00
.. _annref_preremove:
2010-11-02 00:03:50 +03:00
@PreRemove
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Marks a method on the entity to be called as a @PreRemove event.
Only works with @HasLifecycleCallbacks in the entity class PHP
DocBlock.
2010-12-03 22:13:10 +03:00
.. _annref_preupdate:
2010-11-02 00:03:50 +03:00
@PreUpdate
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Marks a method on the entity to be called as a @PreUpdate event.
Only works with @HasLifecycleCallbacks in the entity class PHP
DocBlock.
2010-12-03 22:13:10 +03:00
.. _annref_sequencegenerator:
2010-11-02 00:03:50 +03:00
@SequenceGenerator
~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
For the use with @generatedValue(strategy="SEQUENCE") this
annotation allows to specify details about the sequence, such as
the increment size and initial values of the sequence.
Required attributes:
2011-01-02 13:53:12 +03:00
- **sequenceName** : Name of the sequence
2010-11-01 23:16:12 +03:00
Optional attributes:
2011-01-02 13:53:12 +03:00
- **allocationSize** : Increment the sequence by the allocation size
2010-11-01 23:16:12 +03:00
when its fetched. A value larger than 1 allows to optimize for
scenarios where you create more than one new entity per request.
Defaults to 10
2011-01-02 13:53:12 +03:00
- **initialValue** : Where does the sequence start, defaults to 1.
2010-11-01 23:16:12 +03:00
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Id
* @GeneratedValue(strategy="SEQUENCE")
* @Column(type="integer")
* @SequenceGenerator(sequenceName="tablename_seq", initialValue=1, allocationSize=100)
*/
protected $id = null;
2012-04-15 23:57:44 +04:00
.. _annref_sql_resultset_mapping:
@SqlResultSetMapping
~~~~~~~~~~~~~~~~~~~~
The SqlResultSetMapping annotation is used to specify the mapping of the result of a native SQL query.
The SqlResultSetMapping annotation can be applied to an entity or mapped superclass.
Required attributes:
- **name** : The name given to the result set mapping, and used to refer to it in the methods of the Query API.
Optional attributes:
2012-09-18 12:28:24 +04:00
- **entities** : Array of @EntityResult, Specifies the result set mapping to entities.
2012-04-15 23:57:44 +04:00
- **columns** : Array of @ColumnResult, Specifies the result set mapping to scalar values.
Example:
.. code-block :: php
<?php
/**
* @NamedNativeQueries({
* @NamedNativeQuery(
* name = "fetchUserPhonenumberCount",
* resultSetMapping= "mappingUserPhonenumberCount",
* query = "SELECT id, name, status, COUNT(phonenumber) AS numphones FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username IN (?) GROUP BY id, name, status, username ORDER BY username"
* ),
* @NamedNativeQuery(
* name = "fetchMultipleJoinsEntityResults",
* resultSetMapping= "mappingMultipleJoinsEntityResults",
* query = "SELECT u.id AS u_id, u.name AS u_name, u.status AS u_status, a.id AS a_id, a.zip AS a_zip, a.country AS a_country, COUNT(p.phonenumber) AS numphones FROM cms_users u INNER JOIN cms_addresses a ON u.id = a.user_id INNER JOIN cms_phonenumbers p ON u.id = p.user_id GROUP BY u.id, u.name, u.status, u.username, a.id, a.zip, a.country ORDER BY u.username"
* ),
* })
* @SqlResultSetMappings({
* @SqlResultSetMapping(
* name = "mappingUserPhonenumberCount",
* entities= {
* @EntityResult(
* entityClass = "User",
* fields = {
* @FieldResult(name = "id"),
* @FieldResult(name = "name"),
* @FieldResult(name = "status"),
* }
* )
* },
* columns = {
* @ColumnResult("numphones")
* }
* ),
* @SqlResultSetMapping(
* name = "mappingMultipleJoinsEntityResults",
* entities= {
* @EntityResult(
* entityClass = "__CLASS__",
* fields = {
* @FieldResult(name = "id", column="u_id"),
* @FieldResult(name = "name", column="u_name"),
* @FieldResult(name = "status", column="u_status"),
* }
* ),
* @EntityResult(
* entityClass = "Address",
* fields = {
* @FieldResult(name = "id", column="a_id"),
* @FieldResult(name = "zip", column="a_zip"),
* @FieldResult(name = "country", column="a_country"),
* }
* )
* },
* columns = {
* @ColumnResult("numphones")
* }
* )
*})
*/
class User
{
/** @Id @Column(type="integer") @GeneratedValue * /
public $id;
/** @Column(type="string", length=50, nullable=true) * /
public $status;
/** @Column(type="string", length=255, unique=true) * /
public $username;
/** @Column(type="string", length=255) * /
public $name;
/** @OneToMany(targetEntity="Phonenumber") * /
public $phonenumbers;
/** @OneToOne(targetEntity="Address") * /
public $address;
// ....
}
2010-12-03 22:13:10 +03:00
.. _annref_table:
2010-11-02 00:03:50 +03:00
@Table
~~~~~~~
2010-11-01 23:16:12 +03:00
Annotation describes the table an entity is persisted in. It is
placed on the entity-class PHP DocBlock and is optional. If it is
not specified the table name will default to the entities
unqualified classname.
Required attributes:
2011-01-02 13:53:12 +03:00
- **name** : Name of the table
2010-11-01 23:16:12 +03:00
Optional attributes:
2011-01-02 13:53:12 +03:00
- **indexes** : Array of @Index annotations
- **uniqueConstraints** : Array of @UniqueConstraint annotations.
2010-11-01 23:16:12 +03:00
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Entity
* @Table(name="user",
* uniqueConstraints={@UniqueConstraint(name="user_unique",columns={"username"})},
* indexes={@Index(name="user_idx", columns={"email"})}
* )
*/
class User { }
2010-12-03 22:13:10 +03:00
.. _annref_uniqueconstraint:
2010-11-02 00:03:50 +03:00
@UniqueConstraint
~~~~~~~~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
2010-12-03 22:13:10 +03:00
Annotation is used inside the :ref: `@Table <annref_table>` annotation on
2010-11-01 23:16:12 +03:00
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:
2011-01-02 13:53:12 +03:00
- **name** : Name of the Index
- **columns** : Array of columns.
2010-11-01 23:16:12 +03:00
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
* @Entity
* @Table(name="ecommerce_products",uniqueConstraints={@UniqueConstraint(name="search_idx", columns={"name", "email"})})
*/
class ECommerceProduct
{
}
2010-12-03 22:13:10 +03:00
.. _annref_version:
2010-11-02 00:03:50 +03:00
@Version
~~~~~~~~~~~~~~
2010-11-01 23:16:12 +03:00
Marker annotation that defines a specified column as version
attribute used in an optimistic locking scenario. It only works on
2010-12-03 22:13:10 +03:00
:ref: `@Column <annref_column>` annotations that have the type integer or
datetime. Combining @Version with :ref: `@Id <annref_id>` is not supported.
2010-11-01 23:16:12 +03:00
Example:
2010-12-03 22:13:10 +03:00
.. code-block :: php
2010-11-01 23:16:12 +03:00
<?php
/**
2012-09-18 12:28:24 +04:00
* @Column(type="integer")
* @Version
2010-11-01 23:16:12 +03:00
*/
protected $version;