You are browsing a version that is no longer maintained. |
Attributes Reference
PHP 8 adds native support for metadata with its "Attributes" feature. Doctrine ORM provides support for mapping metadata using PHP attributes as of version 2.9.
The attributes metadata support is closely modelled after the already existing annotation metadata supported since the first version 2.0.
Index
- #[AssociationOverride]
- #[AttributeOverride]
- #[Column]
- #[Cache]
- #[ChangeTrackingPolicy
- #[CustomIdGenerator]
- #[DiscriminatorColumn]
- #[DiscriminatorMap]
- #[Embeddable]
- #[Embedded]
- #[Entity]
- #[GeneratedValue]
- #[HasLifecycleCallbacks]
- #[Index]
- #[Id]
- #[InheritanceType]
- #[JoinColumn]
- #[JoinTable]
- #[ManyToOne]
- #[ManyToMany]
- #[MappedSuperclass]
- #[OneToOne]
- #[OneToMany]
- #[OrderBy]
- #[PostLoad]
- #[PostPersist]
- #[PostRemove]
- #[PostUpdate]
- #[PrePersist]
- #[PreRemove]
- #[PreUpdate]
- #[SequenceGenerator]
- #[Table]
- #[UniqueConstraint]
- #[Version]
Reference
#[AssociationOverride]
In an inheritance hierarchy this attribute allows to override the
assocation mapping definitions of the parent mappings. It needs to be nested
within a #[AssociationOverrides]
on the class level.
Required parameters:
- name: Name of the association mapping to overwrite.
Optional parameters:
- joinColumns: A list of nested
#[JoinColumn]
declarations. - joinTable: A nested
#[JoinTable]
declaration in case of a many-to-many overwrite. - inversedBy: The name of the inversedBy field on the target entity side.
- fetch: The fetch strategy, one of: EAGER, LAZY, EXTRA_LAZY.
Examples:
1 <?php
use Doctrine\ORM\Mapping\AssociationOverride;
use Doctrine\ORM\Mapping\AssociationOverrides;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
#[AssociationOverrides([
new AssociationOverride(
name: "groups",
joinTable: new JoinTable(
name: "ddc964_users_admingroups",
),
joinColumns: [new JoinColumn(name: "adminuser_id")],
inverseJoinColumns: [new JoinColumn(name: "admingroup_id")]
),
new AssociationOverride(
name: "address",
joinColumns: [new JoinColumn(name: "adminaddress_id", referencedColumnName: "id")]
)
])]
class DDC964Admin extends DDC964User
{
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#[AttributeOverride]
In an inheritance hierarchy this attribute allows to override the
field mapping definitions of the parent mappings. It needs to be nested
within a #[AttributeOverrides]
on the class level.
Required parameters:
- name: Name of the association mapping to overwrite.
- column: A nested
#[Column]
attribute with the overwritten field settings.
Examples:
1 <?php
use Doctrine\ORM\Mapping\AttributeOverride;
use Doctrine\ORM\Mapping\AttributeOverrides;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
#[Entity]
#[AttributeOverrides([
new AttributeOverride(
name: "id",
column: new Column(name: "guest_id", type: "integer", length: 140)
),
new AttributeOverride(
name: "name",
column: new Column(name: "guest_name", nullable: false, unique: true, length: 240)
)]
)]
class DDC964Guest extends DDC964User
{
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#[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 parameters:
- type: Name of the DBAL Type which does the conversion between PHP and Database representation.
Optional parameters:
- 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 value for you. - precision: The precision for a decimal (exact numeric) column (applies only for decimal column), which is the maximum number of digits that are stored for the values.
- scale: The scale for a decimal (exact numeric) column (applies only for decimal column), which represents the number of digits to the right of the decimal point and must not be greater than precision.
- 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.
- If not specified, default value is
false
.
- insertable: Boolean value to determine if the column should be included when inserting a new row into the underlying entities table. If not specified, default value is true.
- updatable: Boolean value to determine if the column should be included when updating the row of the underlying entities table. If not specified, default value is true.
- generated: An enum with the possible values ALWAYS, INSERT, NEVER. Is used after an INSERT or UPDATE statement to determine if the database generated this value and it needs to be fetched using a SELECT statement.
-
options: Array of additional options:
default
: The default value to set for the column if no value is supplied.unsigned
: Boolean value to determine if the column should be capable of representing only non-negative integers (applies only for integer column and might not be supported by all vendors).fixed
: Boolean value to determine if the specified length of a string column should be fixed or varying (applies only for string/binary column and might not be supported by all vendors).comment
: The comment of the column in the schema (might not be supported by all vendors).charset
: The charset of the column (only supported by Mysql, PostgreSQL, Sqlite and SQLServer).collation
: The collation of the column (only supported by Mysql, PostgreSQL, Sqlite and SQLServer).check
: Adds a check constraint type to the column (might not be supported by all vendors).
-
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 usecolumnDefinition
.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 #[JoinColumn].
For more detailed information on each attribute, please refer to
the DBAL |
Examples:
1 <?php
use Doctrine\ORM\Mapping\Column;
#[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;
#[Column(type: "string", length: 2, options: [
"fixed" => true,
"comment" => "Initial letters of first and last name"
])]
protected $initials;
#[Column(
type: "integer",
name: "login_count",
nullable: false,
options: ["unsigned" => true, "default" => 0]
)]
protected $loginCount;
// MySQL example: full_name char(41) GENERATED ALWAYS AS (concat(firstname,' ',lastname)),
#[Column(
type: "string",
name: "user_fullname",
insertable: false,
updatable: false
)]
protected $fullname;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#[Cache]
Add caching strategy to a root entity or a collection.
Optional parameters:
- usage: One of
READ_ONLY
,READ_WRITE
orNONSTRICT_READ_WRITE
, By default this isREAD_ONLY
. - region: An specific region name
#[ChangeTrackingPolicy]
The Change Tracking Policy attribute allows to specify how the
Doctrine ORM 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.
The details on all the available change tracking policies can be found in the configuration section.
Example:
#[CustomIdGenerator]
This attribute allows you to specify a user-provided class to generate identifiers. This attribute only works when both #[Id] and #[GeneratedValue(strategy: "CUSTOM")] are specified.
Required parameters:
- class: name of the class which should extend DoctrineORMIdAbstractIdGenerator
Example:
1 <?php
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\CustomIdGenerator;
use App\Doctrine\MyIdGenerator;
#[Id]
#[Column(type: "integer")]
#[GeneratedValue(strategy: "CUSTOM")]
#[CustomIdGenerator(class: MyIdGenerator::class)]
public $id;
2
3
4
5
6
7
8
9
10
11
12
#[DiscriminatorColumn]
This attribute is optional and set on the root entity 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.
If this attribute is not specified, the discriminator column defaults
to a string column of length 255 called dtype
.
Required parameters:
- name: The column name of the discriminator. This name is also used during Array hydration as key to specify the class-name.
Optional parameters:
- type: By default this is string.
- length: By default this is 255.
#[DiscriminatorMap]
The discriminator map is a required attribute on the root entity class in an inheritance hierarchy. Its only argument is an array 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 on whether the classes are in the namespace or not.
1 <?php
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
#[Entity]
#[InheritanceType("JOINED")]
#[DiscriminatorColumn(name: "discr", type: "string")]
#[DiscriminatorMap(["person" => Person::class, "employee" => Employee::class])]
class Person
{
// ...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
#[Embeddable]
The embeddable attribute is required on a class, in order to make it embeddable inside an entity. It works together with the #[Embedded] attribute to establish the relationship between the two classes.
#[Embedded]
The embedded attribute is required on an entity's member variable, in order to specify that it is an embedded class.
Required parameters:
- class: The embeddable class
#[Entity]
Required attribute to mark a PHP class as an entity. Doctrine manages the persistence of all classes marked as entities.
Optional parameters:
- repositoryClass: Specifies the FQCN of a subclass of the
EntityRepository
. Use of repositories for entities is encouraged to keep specialized DQL and SQL operations separated from the Model/Domain Layer. - readOnly: 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: