You are browsing a version that is no longer maintained. |
Annotations Reference
In this chapter a reference of every Doctrine 2 ODM Annotation is given with short explanations on their context and usage.
@AlsoLoad
Specify one or more MongoDB fields to use for loading data if the original field does not exist.
The $fullName
property will be loaded from fullName
if it exists, but
fall back to name
if it does not exist. If multiple fall back fields are
specified, ODM will consider them in order until the first is found.
Additionally, @AlsoLoad may annotate a method with one or more field names. Before normal hydration, the field(s) will be considered in order and the method will be invoked with the first value found as its single argument.
For additional information on using @AlsoLoad, see Migrations.
@ChangeTrackingPolicy
This annotation is used to change the change tracking policy for a document:
For a list of available policies, read the section on change tracking policies.
@DefaultDiscriminatorValue
This annotation can be used when using @DiscriminatorField. It will be used as a fallback value if a document has no discriminator field set. This must correspond to a value from the configured discriminator map.
@DiscriminatorField
This annotation is required for the top-most class in a single collection inheritance hierarchy. It takes a string as its only argument, which specifies the database field to store a class name or key (if a discriminator map is used). ODM uses this field during hydration to select the instantiation class.
For backwards compatibility, the discriminator field may also be specified
via either the |
@DiscriminatorMap
This annotation is required for the top-most class in a
single collection inheritance hierarchy.
It takes an array as its only argument, which maps keys to class names. The
class names must be fully qualified. Using the ::class constant
is supported. When
a document is persisted to the database, its class name key will be stored in
the discriminator field instead of the FQCN. If the discriminator map is non-empty
and it does not contain the class name of the persisted document, a
\Doctrine\ODM\MongoDB\Mapping\MappingException
will be thrown.
@Document
Required annotation to mark a PHP class as a document, whose persistence will be managed by ODM.
Optional attributes:
db
- By default, the document manager will use the MongoDB database defined in the configuration, but this option may be used to override the database for a particular document class.collection
- By default, the collection name is derived from the document's class name, but this option may be used to override that behavior.repositoryClass
- Specifies a custom repository class to use.indexes
- Specifies an array of indexes for this document (deprecated, specify all@Index
annotations on a class level).readOnly
- Prevents document from being updated: it can only be inserted, upserted or removed.writeConcern
- Specifies the write concern for this document that overwrites the default write concern specified in the configuration. It does not overwrite a write concern given as option to the
flush
method when committing your documents.
@EmbedMany
This annotation is similar to @EmbedOne, but instead of embedding one document, it embeds a collection of documents.
Optional attributes:
targetDocument
- A FQCN of the target document.discriminatorField
- The database field name to store the discriminator value within the embedded document.discriminatorMap
- Map of discriminator values to class names.defaultDiscriminatorValue
- A default value for discriminatorField if no value has been set in the embedded document.strategy
- The strategy used to persist changes to the collection. Possible values areaddToSet
,pushAll
,set
, andsetArray
.pushAll
is the default. See Storage Strategies for more information.collectionClass
- A FQCN of class that implementsCollection
interface and is used to hold documents. Doctrine'sArrayCollection
is used by default.notSaved
- The property is loaded if it exists in the database; however, ODM will not save the property value back to the database.
Depending on the embedded document's class, a value of user
or author
will be stored in the type
field and used to reconstruct the proper class
during hydration. The type
field need not be mapped on the embedded
document classes.
@EmbedOne
The @EmbedOne annotation works similarly to @ReferenceOne, except that that document will be embedded within the parent document. Consider the following excerpt from the MongoDB documentation:
The key question in MongoDB schema design is "does this object merit its own collection, or rather should it be embedded within objects in other collections?" In relational databases, each sub-item of interest typically becomes a separate table (unless you are denormalizing for performance). In MongoDB, this is not recommended – embedding objects is much more efficient. Data is then collocated on disk; client-server turnarounds to the database are eliminated. So in general, the question to ask is, "why would I not want to embed this object?"
Optional attributes:
targetDocument
- A FQCN of the target document.discriminatorField
- The database field name to store the discriminator value within the embedded document.discriminatorMap
- Map of discriminator values to class names.defaultDiscriminatorValue
- A default value for discriminatorField if no value has been set in the embedded document.notSaved
- The property is loaded if it exists in the database; however, ODM will not save the property value back to the database.
Depending on the embedded document's class, a value of user
or author
will be stored in the type
field and used to reconstruct the proper class
during hydration. The type
field need not be mapped on the embedded
document classes.
@EmbeddedDocument
Marks the document as embeddable. This annotation is required for any documents to be stored within an @EmbedOne, @EmbedMany or @File\\Metadata relationship.
1 <?php
/** @EmbeddedDocument */
class Money
{
/** @Field(type="float") */
private $amount;
public function __construct(float $amount)
{
$this->amount = $amount;
}
//...
}
/** @Document(db="finance", collection="wallets") */
class Wallet
{
/** @EmbedOne(targetDocument=Money::class) */
private $money;
public function setMoney(Money $money): void
{
$this->money = $money;
}
//...
}
//...
$wallet = new Wallet();
$wallet->setMoney(new Money(34.39));
$dm->persist($wallet);
$dm->flush();
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
Unlike normal documents, embedded documents cannot specify their own database or collection. That said, a single embedded document class may be used with multiple document classes, and even other embedded documents!
Optional attributes:
indexes
- Specifies an array of indexes for this embedded document, to be included in the schemas of any embedding documents (deprecated, specify all@Index
annotations on a class level).
@Field
Marks an annotated instance variable for persistence. Values for this field will be saved to and loaded from the document store as part of the document class' lifecycle.
Optional attributes:
type
- Name of the ODM type, which will determine the value's representation in PHP and BSON (i.e. MongoDB). See Doctrine Mapping Types for a list of types. Defaults to "string".name
- By default, the property name is used for the field name in MongoDB; however, this option may be used to specify a database field name.nullable
- By default, ODM will$unset
fields in MongoDB if the PHP value is null. Specify true for this option to force ODM to store a null value in the database instead of unsetting the field.notSaved
- The property is loaded if it exists in the database; however, ODM will not save the property value back to the database.
Examples:
@File
This marks the document as a GridFS file. GridFS allow storing larger amounts of data than regular documents.
Optional attributes:
db
- By default, the document manager will use the MongoDB database defined in the configuration, but this option may be used to override the database for a particular file.bucketName
- By default, files are stored in a bucket calledfs
. You can customize that bucket name with this property.repositoryClass
- Specifies a custom repository class to use. The class must extend theDoctrine\ODM\MongoDB\Repository\GridFSRepository
interface.indexes
- Specifies an array of indexes for this document (deprecated, specify all@Index
annotations on a class level).readOnly
- Prevents the file from being updated: it can only be inserted, upserted or removed.writeConcern
- Specifies the write concern for this file that overwrites the default write concern specified in the configuration.
@FileChunkSize
This maps the chunkSize
property of a GridFS file to a property. It contains
the size of a single file chunk in bytes. No other options can be set.
@FileFilename
This maps the filename
property of a GridFS file to a property. No other
options can be set.
@FileLength
This maps the length
property of a GridFS file to a property. It contains
the size of the entire file in bytes. No other options can be set.
@FileMetadata
This maps the metadata
property of a GridFS file to a property. Metadata can
be used to store additional properties in a file. The metadata document must be
an embedded document mapped using @EmbeddedDocument.
Optional attributes:
targetDocument
- A FQCN of the target document.discriminatorField
- The database field name to store the discriminator value within the embedded document.discriminatorMap
- Map of discriminator values to class names.defaultDiscriminatorValue
- A default value fordiscriminatorField
if no value has been set in the embedded document.
@FileUploadDate
This maps the uploadDate
property of a GridFS file to a property. No other
options can be set.
@HasLifecycleCallbacks
This annotation must be set on the document class to instruct Doctrine to check for lifecycle callback annotations on public methods. Using @PreFlush, @PreLoad, @PostLoad, @PrePersist, @PostPersist, @PreRemove, @PostRemove, @PreUpdate, or @PostUpdate on methods without this annotation will cause Doctrine to ignore the callbacks.
@Id
The annotated instance variable will be marked as the document identifier. The default behavior is to store an MongoDB\BSON\ObjectId instance, but you may customize this via the strategy attribute.
@Index
This annotation is used to specify indexes to be created on the collection (or embedding document's collection in the case of @EmbeddedDocument). It may also be used at the property-level to define single-field indexes.
Optional attributes:
keys
- Mapping of indexed fields to their ordering or index type. ODM will allowasc
anddesc
to be used in place of1
and-1
, respectively. Special index types (e.g.2dsphere
) should be specified as strings. This is required when @Index is used at the class level.options
- Options for creating the index. Options are documented in the indexes chapter.
The keys
and options
attributes correspond to the arguments for
MongoDB\Collection::createIndex().
ODM allows mapped field names (i.e. PHP property names) to be used when defining
keys
.
If you are creating a single-field index, you can simply specify an @Index or @UniqueIndex on a mapped property:
If the |
@Indexes
The |
This annotation may be used at the class level to specify an array of @Index
annotations. It is functionally equivalent to specifying multiple @Index
annotations on a class level.
@InheritanceType
This annotation must appear on the top-most class in an
inheritance hierarchy. SINGLE_COLLECTION
and
COLLECTION_PER_CLASS
are currently supported.
Examples:
1 <?php
/**
* @Document
* @InheritanceType("COLLECTION_PER_CLASS")
*/
class Person
{
// ...
}
/**
* @Document
* @InheritanceType("SINGLE_COLLECTION")
* @DiscriminatorField("type")
* @DiscriminatorMap({"person"=Person::class, "employee"=Employee::class})
*/
class Person
{
// ...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Lock
The annotated instance variable will be used to store lock information for pessimistic locking.
This is only compatible with the int
type, and cannot be combined with @Id.
@MappedSuperclass
The annotation is used to specify classes that are parents of document classes and should not be managed directly. See inheritance mapping for additional information.
@PostLoad
Marks a method on the document class to be called on the postLoad
event. The
@HasLifecycleCallbacks annotation must be present on the same class for the
method to be registered.
See Lifecycle Events for more information.
@PostPersist
Marks a method on the document class to be called on the postPersist
event.
The @HasLifecycleCallbacks annotation must be present on the same class for
the method to be registered.
See Lifecycle Events for more information.
@PostRemove
Marks a method on the document class to be called on the postRemove
event.
The @HasLifecycleCallbacks annotation must be present on the same class for
the method to be registered.
See Lifecycle Events for more information.
@PostUpdate
Marks a method on the document class to be called on the postUpdate
event.
The @HasLifecycleCallbacks annotation must be present on the same class for
the method to be registered.
See Lifecycle Events for more information.
@PreFlush
Marks a method on the document class to be called on the preFlush
event. The
@HasLifecycleCallbacks annotation must be present on the same class for the
method to be registered.
See Lifecycle Events for more information.
@PreLoad
Marks a method on the document class to be called on the preLoad
event. The
@HasLifecycleCallbacks annotation must be present on the same class for the
method to be registered.
See Lifecycle Events for more information.
@PrePersist
Marks a method on the document class to be called on the prePersist
event.
The @HasLifecycleCallbacks annotation must be present on the same class for
the method to be registered.
See Lifecycle Events for more information.
@PreRemove
Marks a method on the document class to be called on the preRemove
event.
The @HasLifecycleCallbacks annotation must be present on the same class for
the method to be registered.
See Lifecycle Events for more information.
@PreUpdate
Marks a method on the document class to be called on the preUpdate
event.
The @HasLifecycleCallbacks annotation must be present on the same class for
the method to be registered.
See Lifecycle Events for more information.
@ReadPreference
Specifies Read Preference <https://docs.mongodb.com/manual/core/read-preference/>_
that will be applied when querying for the annotated document.
@ReferenceMany
Defines that the annotated instance variable holds a collection of referenced documents.
Optional attributes:
targetDocument
- A FQCN of the target document. AtargetDocument
is required when usingstoreAs: id
.storeAs
- Indicates how to store the reference.id
stores the identifier,ref
an embedded object containing theid
field and (optionally) a discriminator.dbRef
anddbRefWithDb
store a DBRef object and are deprecated in favor ofref
. Note thatid
references are not compatible with the discriminators.cascade
- Cascade OptiondiscriminatorField
- The field name to store the discriminator value within the reference object.discriminatorMap
- Map of discriminator values to class names.defaultDiscriminatorValue
- A default value fordiscriminatorField
if no value has been set in the referenced document.inversedBy
- The field name of the inverse side. Only allowed on owning side.mappedBy
- The field name of the owning side. Only allowed on the inverse side.repositoryMethod
- The name of the repository method to call to populate this reference.sort
- The default sort for the query that loads the reference.criteria
- Array of default criteria for the query that loads the reference.limit
- Limit for the query that loads the reference.skip
- Skip for the query that loads the reference.strategy
- The strategy used to persist changes to the collection. Possible values areaddToSet
,pushAll
,set
, andsetArray
.pushAll
is the default. See Storage Strategies for more information.collectionClass
- A FQCN of class that implementsCollection
interface and is used to hold documents. Doctrine'sArrayCollection
is used by defaultprime
- A list of references contained in the target document that will be initialized when the collection is loaded. Only allowed for inverse references.notSaved
- The property is loaded if it exists in the database; however, ODM will not save the property value back to the database.
1 <?php
/**
* @ReferenceMany(
* strategy="set",
* targetDocument=Documents\Item::class,
* cascade="all",
* sort={"sort_field": "asc"}
* discriminatorField="type",
* discriminatorMap={
* "book"=Documents\BookItem::class,
* "song"=Documents\SongItem::class
* },
* defaultDiscriminatorValue="book"
* )
*/
private $cart;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@ReferenceOne
Defines an instance variable holds a related document instance.
Optional attributes:
targetDocument
- A FQCN of the target document. AtargetDocument
is required when usingstoreAs: id
.storeAs
- Indicates how to store the reference.id
stores the identifier,ref
an embedded object containing theid
field and (optionally) a discriminator.dbRef
anddbRefWithDb
store a DBRef object and are deprecated in favor ofref
. Note thatid
references are not compatible with the discriminators.cascade
- Cascade OptiondiscriminatorField
- The field name to store the discriminator value within the reference object.discriminatorMap
- Map of discriminator values to class names.defaultDiscriminatorValue
- A default value fordiscriminatorField
if no value has been set in the referenced document.inversedBy
- The field name of the inverse side. Only allowed on owning side.mappedBy
- The field name of the owning side. Only allowed on the inverse side.repositoryMethod
- The name of the repository method to call to populate this reference.sort
- The default sort for the query that loads the reference.criteria
- Array of default criteria for the query that loads the reference.limit
- Limit for the query that loads the reference.skip
- Skip for the query that loads the reference.notSaved
- The property is loaded if it exists in the database; however, ODM will not save the property value back to the database.
1 <?php
/**
* @ReferenceOne(
* targetDocument=Documents\Item::class,
* cascade="all",
* discriminatorField="type",
* discriminatorMap={
* "book"=Documents\BookItem::class,
* "song"=Documents\SongItem::class
* },
* defaultDiscriminatorValue="book"
* )
*/
private $cart;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@ShardKey
This annotation may be used at the class level to specify a shard key to be used for sharding the document collection.
@Validation
This annotation may be used at the class level to specify the validation schema for the related collection.
validator
- Specifies a schema that will be used by MongoDB to validate data inserted or updated in the collection. Please refer to the following MongoDB documentation (Schema Validation ¶) for more details. The value should be a string representing a BSON document under the Extended JSON specification. The recommended way to fill up this property is to create a class constant (eg.::VALIDATOR
) using the HEREDOC/NOWDOC syntax for clarity and to reference it as the annotation value. Please note that if you decide to insert the schema directly in the annotation without using a class constant then double quotes"
have to be escaped by doubling them""
. This method also requires that you don't prefix multiline strings by the Docblock asterisk symbol*
.-
action
- Determines how MongoDB handles documents that violate the validation rules. Please refer to the related MongoDB documentation (Accept or Reject Invalid Documents ¶) for more details. The allowed values are the following:error
warn
If it is not defined then the default behavior (
error
) will be used. Those values are also declared as constants for convenience:\Doctrine\ODM\MongoDB\Mapping\ClassMetadata::SCHEMA_VALIDATION_ACTION_ERROR
\Doctrine\ODM\MongoDB\Mapping\ClassMetadata::SCHEMA_VALIDATION_ACTION_WARN
Import the
ClassMetadata
namespace to use those constants in your annotation. -
level
- Determines which operations MongoDB applies the validation rules. Please refer to the related MongoDB documentation (Existing Documents ¶) for more details. The allowed values are the following:off
strict
moderate
If it is not defined then the default behavior (
strict
) will be used. Those values are also declared as constants for convenience:\Doctrine\ODM\MongoDB\Mapping\ClassMetadata::SCHEMA_VALIDATION_LEVEL_OFF
\Doctrine\ODM\MongoDB\Mapping\ClassMetadata::SCHEMA_VALIDATION_LEVEL_STRICT
\Doctrine\ODM\MongoDB\Mapping\ClassMetadata::SCHEMA_VALIDATION_LEVEL_MODERATE
Import the
ClassMetadata
namespace to use those constants in your annotation.
1 <?php
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
// ... other imports
/**
* @Document
* @Validation(
* validator=SchemaValidated::VALIDATOR,
* action=ClassMetadata::SCHEMA_VALIDATION_ACTION_WARN,
* level=ClassMetadata::SCHEMA_VALIDATION_LEVEL_MODERATE,
* )
*/
class SchemaValidated
{
public const VALIDATOR = <<<'EOT'
{
"$jsonSchema": {
"required": ["name"],
"properties": {
"name": {
"bsonType": "string",
"description": "must be a string and is required"
}
}
},
"$or": [
{ "phone": { "$type": "string" } },
{ "email": { "$regex": { "$regularExpression" : { "pattern": "@mongodb\\.com$", "options": "" } } } },
{ "status": { "$in": [ "Unknown", "Incomplete" ] } }
]
}
EOT;
// rest of the class code...
}
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
35
36
@Version
The annotated instance variable will be used to store version information for optimistic locking.
This is only compatible with types implementing the \Doctrine\ODM\MongoDB\Types\Versionable
interface and cannot be
combined with @Id. Following ODM types can be used for versioning: int
, decimal128
, date
, and
date_immutable
.
By default, Doctrine ODM updates embed-many and reference-many collections in separate write operations, which do not bump the document version. Users employing document versioning are encouraged to use the atomicSet or atomicSetArray strategies for such collections, which will ensure that collections are updated in the same write operation as the versioned parent document.
@View
Required annotation to mark a PHP class as a view. Views are created from aggregation pipelines, which are returned from a special repository method. Views can be used like collections for any read operations. Result documents are not managed and cannot be referenced using the reference-many and reference-one mappings.
Required attributes:
rootClass
- this is the base collection that the view is created fromrepositoryClass
- a repository class is required. This repository must implement theMongoDB\ODM\MongoDB\Repository\ViewRepository
interface.
Optional attributes:
db
- By default, the document manager will use the MongoDB database defined in the configuration, but this option may be used to override the database for a particular document class.view
- By default, the view name is derived from the document's class name, but this option may be used to override that behavior.
1 <?php
/**
* @View(
* db="documents",
* rootClass=User::class,
* repositoryClass=UserNameRepository::class,
* )
*/
class UserName
{
//...
}
class UserNameRepository implements \Doctrine\ODM\MongoDB\Repository\ViewRepository
{
public function createViewAggregation(Builder $builder) : void
{
$builder->project()
->includeFields(['username']);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
The createViewAggregation
method can add any aggregation pipeline stage,
except for the $out
and $merge
stages. The pipeline is created for the
root class specified in the view mapping.
Views must be created before they can be queried. This can be done using the
|