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. .. code-block:: php firstName, $this->lastName) = explode(' ', $name); } For additional information on using `@AlsoLoad`_, see :doc:`Migrations `. @Bin ---- Alias of `@Field`_, with "type" attribute set to "bin". Converts value to `MongoBinData`_ with ``MongoBinData::GENERIC`` sub-type. .. code-block:: php `. @Collection ----------- Alias of `@Field`_, with "type" attribute set to "collection". Stores and retrieves the value as a numerically indexed array. .. code-block:: php ` 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. .. code-block:: php ` hierarchy. It takes an array as its only argument, which maps keys to class names. The class names may be fully qualified or relative to the current namespace. When a document is persisted to the database, its class name key will be stored in the discriminator field instead of the |FQCN|. .. code-block:: php ` query method to populate the property with the calculated distance value. .. code-block:: php dm->createQuery('City') ->geoNear(50, 60) ->limit(1) ->getQuery() ->getSingleResult(); echo $city->distance; @Document --------- Required annotation to mark a PHP class as a document, whose peristence 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. - readOnly - Prevents document from being updated: it can only be inserted, upserted or removed. - requireIndexes - Specifies whether or not queries for this document should require indexes by default. This may also be specified per query. - 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 :ref:`option ` to the ``flush`` method when committing your documents. .. code-block:: php amount = (float) $amount; } //... } /** @Document(db="finance", collection="wallets") */ class Wallet { /** @EmbedOne(targetDocument="Money") */ private $money; public function setMoney(Money $money) { $this->money = $money; } //... } //... $wallet = new Wallet(); $wallet->setMoney(new Money(34.39)); $dm->persist($wallet); $dm->flush(); 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. @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 :ref:`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. Examples: .. code-block:: php ` attribute. .. code-block:: php downloads++; } // ... } Now, update a Package instance like so: .. code-block:: php incrementDownloads(); $dm->flush(); The query sent to Mongo would resemble the following: .. code-block:: json { "$inc": { "downloads": 1 } } The field will be incremented by the difference between the new and old values. This is useful if many requests are attempting to update the field concurrently. .. note:: This annotation is deprecated and will be removed in ODM 2.0. Please use the `@Field`_ annotation with type "int" or "float" and use the "increment" strategy. @Index ------ This annotation is used inside of the class-level `@Document`_ or `@EmbeddedDocument`_ annotations 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 allow "asc" and "desc" to be used in place of ``1`` 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 The ``keys`` and ``options`` attributes correspond to the arguments for `MongoCollection::createIndex() `_. ODM allows mapped field names (i.e. PHP property names) to be used when defining ``keys``. .. code-block:: php `. ``SINGLE_COLLECTION`` and ``COLLECTION_PER_CLASS`` are currently supported. Examples: .. code-block:: php `. This is only compatible with the ``int`` type, and cannot be combined with `@Id`_. .. code-block:: php ` for additional information. .. code-block:: php _` that will be applied when querying for the annotated document. .. code-block:: php `. This is only compatible with ``int`` and ``date`` field types, and cannot be combined with `@Id`_. .. code-block:: php ` and :ref:`reference-many ` collections in separate write operations, which do not bump the document version. Users employing document versioning are encouraged to use the :ref:`atomicSet ` or :ref:`atomicSetArray ` strategies for such collections, which will ensure that collections are updated in the same write operation as the versioned parent document. .. _BSON specification: http://bsonspec.org/spec.html .. _DBRef: https://docs.mongodb.com/manual/reference/database-references/#dbrefs .. _geoNear command: https://docs.mongodb.com/manual/reference/command/geoNear/ .. _GridFS: https://docs.mongodb.com/manual/core/gridfs/ .. _MongoBinData: http://php.net/manual/en/class.mongobindata.php .. _MongoDate: http://php.net/manual/en/class.mongodate.php .. _MongoGridFSFile: http://php.net/manual/en/class.mongogridfsfile.php .. _MongoId: http://php.net/manual/en/class.mongoid.php .. _MongoMaxKey: http://php.net/manual/en/class.mongomaxkey.php .. _MongoMinKey: http://php.net/manual/en/class.mongominkey.php .. _MongoTimestamp: http://php.net/manual/en/class.mongotimestamp.php .. |FQCN| raw:: html FQCN