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]
protected $id;
#[PHPCR\ParentDocument]
protected $parent;
#[PHPCR\Nodename]
protected $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 thephpcr:node-type:register
console command (usehelp 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 thedoctrine:phpcr:mapping:verify-unique-node-types
command. - repositoryClass: Name of the repository to use for this document.
- versionable: (string) Set to
simple
orfull
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
orchild
. 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 |
#[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
orchild
. See language mapping.
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 thetranslator
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')]
protected $author;
#[PHPCR\Field(type: 'string', translated: true)]
protected $title;
#[PHPCR\Field(type: 'string', translated: true, nullable: true)]
protected $subTitle;
#[PHPCR\Field(type: 'boolean')]
protected $enabled;
#[PHPCR\Field(type: 'string', multivalue: true)]
protected $keywords; // e.g. ['dog', 'cat', 'mouse']
#[PHPCR\Field(type: 'double', assoc: '')]
protected $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:
- cascade: One of persist, remove, merge, detach, refresh, translation or all. See Transitive persistence / Cascade Operations
#[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
#[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 $depth;
#[ParentDocument]
Optional parameters:
- cascade: One of persist, remove, merge, detach, refresh, translation or all. See Transitive persistence / Cascade Operations
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 $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
orPARENT
, default isPARENT
See generation strategies.
#[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]
protected $id; // e.g. /path/to/mydocument
#[PHPCR\Nodename]
protected $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]
protected $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 |
#[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()
{
// ... 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()
{
// ... 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()
{
// ... 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()
{
// ... 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()
{
// ... 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()
{
// ... 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()
{
// ... do something before the document has been updated
}
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
orpath
. See reference other documents.
#[ReferenceOne]
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
orpath
. See reference other documents. - cascade: One of persist, remove, merge, detach, refresh, translation or all. See Transitive persistence / Cascade Operations
#[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:
- cascade: One of persist, remove, merge, detach, refresh, translation or all. See Transitive persistence / Cascade Operations
#[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
orhard
.
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]
protected $locale;
#[PHPCR\Field(type: 'string', translated: true)]
protected $title;
}
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 $versionName;
#[PHPCR\VersionCreated]
private $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.