You are browsing a version that is no longer maintained.

Attributes Mapping

In this chapter a reference of every PHPCR-ODM attribute is listed. with short explanations on their context and usage.

Note on usage

Note that the code examples are given without their namespaces, however it is normally necessary to import the attribute namespace into your class, and to prefix each attribute with the namespace as demonstrated in the following example:

namespace MyProject\Bundle\BlogBundle\Document;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Document]
class Post
{
    #[PHPCR\Id]
    private string $id;

    #[PHPCR\ParentDocument]
    private object $parent;

    #[PHPCR\Nodename]
    private string $title;
}

Document

#[Document]

Optional parameters:

  • nodeType: PHPCR type for this node, default nt:unstructured.
  • uniqueNodeType: If this document has a unique node type, set to true in order to support outer joins correctly. See left outer join and right outer join. To register a custom node type, use the phpcr:node-type:register console command (use help phpcr:node-type:register for the syntax; see Tools for more information). To verify that documents claiming to have unique node types are truly unique, use the doctrine:phpcr:mapping:verify-unique-node-types command.
  • repositoryClass: Name of the repository to use for this document.
  • versionable: (string) Set to simple or full to enable versioning (respectively simple or full level), false to disable versioning inheritance. Implies referenceable. Note that not every PHPCR implementation support this feature. See Versioning.
  • referenceable: Set to true to allow this node to be referenced.
  • translator: Determines how translations are stored, one of attribute or child. See langauge mapping
  • mixins: Optional list of PHPCR mixins that will be added to the node on creation. Note that if this field is present, it overwrites the same field from the anchestor documents so you have to repeat mixins you want to keep if you add a mixins field.
  • childClasses: List of valid child classes (if empty any classes are permitted).
  • isLeaf: If the document should act as a leaf (i.e. it can have no children). Mutually exclusive with childClasses.

Minimal example:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Document]
class User
{
    // ...
}

Full example:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Document(
    repositoryClass: UserRepository::class,
    versionable: 'full',
    referenceable: true,
    translator: 'child',
    mixins: ['mix:created', 'mix:lastModified'],
    childClasses: [Article::class, Page::class]
)]
class Article
{
    // ...
}

The uniqueNodeType attribute is not supported with the sqlite database.

#[MappedSuperclass]

A mapped superclass is an abstract or concrete class that provides persistent document state and mapping information for its subclasses but which is not itself a document.

Contrary to ORM, the PHPCR-ODM with its NoSQL nature can handle documents that extend each other just like any other document, so you only need mapped superclasses in special situations. See also Inheritance Mapping.

Optional parameters:

  • nodeType: PHPCR type for this node. Default nt:unstructured.
  • repositoryClass: Fully qualified name of the repository to use for documents extending this superclass.
  • translator: Determines how translations are stored, one of attribute or child. See language mapping.
1use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR; #[PHPCR\MappedSuperclass] class MappedSuperclassBase { // ... fields and methods } #[PHPCR\Document] class DocumentSubClassFoo extends MappedSuperclassBase { // ... fields and methods }
2
3
4
5
6
7
8
9
10
11
12
13

Mapping Fields

You can attribute an instance variable with the #[Field] attributeto make it "persistent".

#[Field]

parameters:

  • property: The PHPCR property name to which this field is stored. Defaults to the field name.
  • assoc: Specify that this attribute should be an associative array. The value should be a string which will be used by the PHPCR node. Set to an empty string to automatically use the name of the property with that attribute appended by "Keys".
  • multivalue: true to specify that this property should be treated as a simple array. See Mapping multivalue properties.
  • translated: true to specify that the property should be translatable, requires the translator attribute to be specified in #[Document].
  • nullable: true to specifiy that this property doesn't have a required value, used when loading a translation, to allow loading a node with a missing translated property.
  • type: Type of the field, see table below.

Types:

  • binary: Sets the type of the property to binary.
  • boolean: Sets the type of the property to boolean.
  • date: Sets the type of the property to DateTime.
  • decimal: Sets the type of the property to decimal, the decimal field uses the BCMath library which supports numbers of any size or precision.
  • double: Sets the type of the property to double. The PHP type will be float.
  • long: Sets the type of the property to long. The PHP type will be integer.
  • name: The property must be a valid XML CNAME value and can be used to store a valid node name.
  • path: The property must be a valid PHPCR node path and can be used to store an arbitrary reference to another node.
  • string: Sets the type of the property to string.
  • uri: The property will be validated as an URI.

Examples:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Field(type: 'string')]
private string $author;

#[PHPCR\Field(type: 'string', translated: true)]
private string $title;

#[PHPCR\Field(type: 'string', translated: true, nullable: true)]
private ?string $subTitle;

#[PHPCR\Field(type: 'boolean')]
private bool $enabled;

#[PHPCR\Field(type: 'string', multivalue: true)]
private array $keywords; // e.g. ['dog', 'cat', 'mouse']

#[PHPCR\Field(type: 'double', assoc: '')]
private array $exchangeRates; // e.g. ['GBP' => 0.810709, 'EUR' => 1, 'USD' => 1.307460]

Hierarchy

These mappings mark the properties to contain instances of Documents above or below the current Document in the document hierarchy, or information about the state of the document within the hierarchy. They need to be specified inside the instance variables associated PHP DocBlock comment.

#[Child]

The property will be populated with the named document directly below the instance variables document class in the document hierarchy.

Required parameters:

  • nodeName: PHPCR Node name of the child document to map, this should be a string.

Optional parameters:

1use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR; #[PHPCR\Child(name: 'Preferences')] private object $preferences;
2
3
4

#[Children]

The property will be populated with Documents directly below the instance variables document class in the document hierarchy.

Optional parameters:

  • filter: Child name filter; only return children whose names match the given filter.
  • fetchDepth: Performance optimisation, number of levels to pre-fetch and cache, this should be an integer.
  • ignoreUntranslated: Set to false to not throw exceptions on untranslated child documents.
  • cascade: One of persist, remove, merge, detach, refresh, translation or all. See Transitive persistence / Cascade Operations
1use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR; use Doctrine\ODM\PHPCR\ChildrenCollection; #[PHPCR\Children(filter: 'a*', fetchDepth: 3)] private ChildrenCollection $children;
2
3
4
5

#[Depth]

The property will be populated with an integer value representing the depth of the document within the document hierarchy:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Depth]
private int $depth;

#[ParentDocument]

Optional parameters:

The property will contain the nodes parent document. Assigning a different parent will result in a move operation:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\ParentDocument]
private object $parent;

Identification

These mappings help to manage the identification of the document class.

#[Id]

The property will be marked with the documents identifier. The ID is the full path to the document in the document hierarchy. See identifiers.

Required parameters:

  • strategy: How to generate IDs, one of NONE, REPOSITORY, ASSIGNED or PARENT, default is PARENT See generation strategies.
1use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR; #[PHPCR\Id] private string $id; // e.g. /path/to/mydocument
2
3
4

#[Nodename]

Mark the property as representing the name of the node. The name of the node is the last part of the ID. Changing the marked variable will update the nodes ID:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Id]
private string $id; // e.g. /path/to/mydocument

#[PHPCR\Nodename]
private string $nodeName; // e.g. mydocument

#[Uuid]

The property will be populated with a UUID (Universally Unique Identifier). The UUID is immutable. For this field to be reliably populated the document should be referenceable:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Uuid]
private string $uuid; // e.g. 508d6621-0c20-4972-bf0e-0278ccabe6e5

Lifcycle callbacks

These attributes are used to map information on methods of a document. The method is called automatically by the ODM on the lifecycle event corresponding to the attribute.

Unlike the Doctrine ORM it is not necessary to specify a #[HasLifecycleCallbacks] attribute.

#[PostLoad]

Life cycle callback. The marked method will be called automatically on the postLoad event. See lifecycle callbacks for further explanations:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

 #[PHPCR\PostLoad]
 public function doSomethingOnPostLoad(): void
 {
    // ... do something after the Document has been loaded
 }

#[PostPersist]

Life cycle callback. The marked method will be called automatically on the postPersist event. See lifecycle callbacks for further explanations:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

 #[PHPCR\PostPersist]
 public function doSomethingOnPostPersist(): void
 {
   // ... do something after the document has been persisted
 }

#[PostRemove]

Life cycle callback. The marked method will be called automatically on the postRemove event. See lifecycle callbacks for further explanations:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

 #[PHPCR\PostRemove]
 public function doSomethingOnPostRemove(): void
 {
   // ... do something after the document has been removed
 }

#[PostUpdate]

Life cycle callback. The marked method will be called automatically on the postUpdate event. See lifecycle callbacks for further explanations:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

 #[PHPCR\PostUpdate]
 public function doSomethingOnPostUpdate(): void
 {
   // ... do something after the document has been updated
 }

#[PrePersist]

Life cycle callback. The marked method will be called automatically on the prePersist event. See lifecycle callbacks for further explanations:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

 #[PHPCR\PrePersist]
 public function doSomethingOnPrePersist(): void
 {
   // ... do something before the document has been persisted
 }

#[PreRemove]

Life cycle callback. The marked method will be called automatically on the preRemove event. See lifecycle callbacks for further explanations:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

 #[PHPCR\PreRemove]
 public function doSomethingOnPreRemove(): void
 {
   // ... do something before the document has been removed
 }

#[PreUpdate]

Life cycle callback. The marked method will be called automatically on the preUpdate event. See lifecycle callbacks for further explanations:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

 #[PHPCR\PreUpdate]
 public function doSomethingOnPreUpdate(): void
 {
   // ... do something before the document has been updated
 }

PHPCR

#[Node]

The property will be populated with the underlying PHPCR node. See node field mapping.

References

#[ReferenceMany]

Optional parameters:

  • targetDocument: Specify type of target document class. Note that this is an optional parameter and by default you can associate any document.
  • strategy: One of weak, hard or path. See reference other documents.
1use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR; use Doctrine\ODM\PHPCR\ReferenceManyCollection; #[PHPCR\ReferenceMany(targetDocument: PhoneNumber::class, strategy: 'hard')] private ReferenceManyCollection $phoneNumbers;
2
3
4
5

#[ReferenceOne]

Optional parameters:

1use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR; #[PHPCR\ReferenceOne(targetDocument: Contact::class, strategy: 'hard')] private object $contact;
2
3
4

#[Referrers]

Mark the property to contain a collection of the documents of the given document class which refer to this document.

Required parameters:

  • referringDocument: Full class name of referring document, the instances of which should be collected in the property.
  • referencedBy: Name of the property from the referring document class which refers to this document class.

Optional parameters:

1use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR; use Doctrine\ODM\PHPCR\ReferrersCollection; #[PHPCR\Referrers(referringDocument: Address::class, referencedBy: 'addressbook')] private ReferrersCollection $addresses;
2
3
4
5

#[MixedReferrers]

Mark the property to hold a collection of all documents which refer to this document, regardless of document class.

Optional parameters:

  • referenceType: One of weak or hard.
1use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR; use Doctrine\ODM\PHPCR\ReferrersCollection; #[PHPCR\MixedReferrers] private ReferrersCollection $referrers;
2
3
4
5

Translation

These attributes only apply to documents where the translator attribute is specified in #[Document].

Example:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Document(translator: 'attribute')]
class MyDocument
{
   #[PHPCR\Locale]
   private string $locale;

   #[PHPCR\Field(type: 'string', translated: true)]
   private string $title;
}

#[Locale]

Identifies the property as the field in which to store the documents current locale.

Versioning

These attributes only apply to documents where the versionable attribute is specified in #[Document].

See versioning mappings.

Example:

use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

#[PHPCR\Document(versionable: 'simple')]
class MyPersistentClass
{
    #[PHPCR\VersionName]
    private string $versionName;

    #[PHPCR\VersionCreated]
    private \DateTimeInterface $versionCreated;
}

#[VersionCreated]

The property will be populated with the date that the current document version was created. Applies only to documents with the versionable attribute.

#[VersionName]

The property will be populated with the name of the current version as given by PHPCR.