Skip to content

Commit

Permalink
Merge pull request #852 from doctrine/annotations
Browse files Browse the repository at this point in the history
make annotations also attributes
  • Loading branch information
dbu authored Dec 3, 2023
2 parents dedb884 + 82d859e commit 68f0da2
Show file tree
Hide file tree
Showing 112 changed files with 1,901 additions and 566 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog
1.8.0 (unreleased)
------------------

* Add support for PHP attributes (only for the annotations that are not deprecated)
* Drop support for PHP 7.

1.7.2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# PHPCR ODM for Doctrine2
# PHPCR ODM for Doctrine

[![Build Status](https://github.com/doctrine/phpcr-odm/actions/workflows/test-application.yaml/badge.svg?branch=1.x)](https://github.com/doctrine/phpcr-odm/actions/workflows/test-application.yaml)
[![Latest Stable Version](https://poser.pugx.org/doctrine/phpcr-odm/version.png)](https://packagist.org/packages/doctrine/phpcr-odm)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"doctrine/annotations": "^1.14.3 || ^2.0",
"doctrine/data-fixtures": "^1.0",
"doctrine/event-manager": "^1.0 || ^2.0",
"doctrine/persistence": "^1.3.7 || ^2.0",
"doctrine/persistence": "^2.4",
"phpcr/phpcr": "^2.1.1",
"phpcr/phpcr-implementation": "^2.1",
"phpcr/phpcr-utils": "^1.3.0",
Expand Down
8 changes: 6 additions & 2 deletions docs/en/reference/annotations-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Annotation Mapping
In this chapter a reference of every PHPCR-ODM annotation is given with short
explanations on their context and usage.

.. warning::

Annotations have been deprecated in favor of :doc:`Attributes<attributes-mapping>`

Note on usage
-------------

Expand Down Expand Up @@ -230,12 +234,12 @@ Examples::
/**
* @PHPCR\Field(type="string", multivalue=true)
*/
protected $keywords; // e.g. array('dog', 'cat', 'mouse')
protected $keywords; // e.g. ['dog', 'cat', 'mouse']

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

Hierarchy
---------
Expand Down
106 changes: 32 additions & 74 deletions docs/en/reference/association-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Hierarchy mappings
------------------

We have already seen the ``ParentDocument`` in the previous chapter in the section about
identifier generation. The field with this annotation maps the parent document of this document
identifier generation. The field with this mapping contains the parent document of this document
(``PHPCR\NodeInterface::getParent()``). If the repository can determine the document class of the
parent, it will use it, otherwise ``Doctrine\ODM\PHPCR\Document\Generic`` is used.

Expand Down Expand Up @@ -51,21 +51,15 @@ Some sample mappings:

.. code-block:: php
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;
/**
* @PHPCR\Parentdocument
*/
#[PHPCR\ParentDocument]
private $parent;
/**
* @PHPCR\Child
*/
#[PHPCR\Child]
private $mychild;
/**
* @PHPCR\Children(filter="a*", fetchDepth=3)
*/
#[PHPCR\Children(filter: 'a*', fetchDepth: 3)]
private $children;
.. code-block:: xml
Expand Down Expand Up @@ -100,9 +94,7 @@ document is not allowed to have children (i.e. that it is a leaf node).
.. code-block:: php
<?php
/**
* @Document(childClasses={"App\Documents\Article", "App\Documents\Page"})
*/
#[Document(childClasses: [Article::class, Page::class])]
class ContentFolder
{
// ...
Expand All @@ -112,8 +104,8 @@ document is not allowed to have children (i.e. that it is a leaf node).
<doctrine-mapping>
<document class="ContentFolder">
<child-class>Article</child-class>
<child-class>Page</child-class>
<child-class>Fqn\Article</child-class>
<child-class>Fqn\Page</child-class>
<!-- ... -->
</document>
</doctrine-mapping>
Expand All @@ -122,7 +114,7 @@ document is not allowed to have children (i.e. that it is a leaf node).
ContentFolder:
# ...
child_classes: [ "Article", "Page" ]
child_classes: [ "Fqn\Article", "Fqn\Page" ]
To specify that a document can have no children:

Expand All @@ -131,9 +123,7 @@ To specify that a document can have no children:
.. code-block:: php
<?php
/**
* @Document(isLeaf=true)
*/
#[Document(isLeaf: true)]
class LeafDocument
{
// ...
Expand Down Expand Up @@ -183,16 +173,12 @@ id standard and is guaranteed to be unique for the whole PHPCR repository (all w

.. code-block:: php
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;
/**
* @PHPCR\Document(referenceable=true)
*/
#[PHPCR\Document(referenceable: true)]
class MyPersistentClass
{
/**
* @PHPCR\Uuid
**/
#[PHPCR\Uuid]
private $uuid;
}
Expand Down Expand Up @@ -242,36 +228,24 @@ A path reference will never ensure referential integrity.

.. code-block:: php
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;
/**
* @PHPCR\ReferenceOne(strategy="weak")
*/
#[PHPCR\ReferenceOne(strategy: 'weak')]
private $weakTarget;
/**
* @PHPCR\ReferenceOne(strategy="hard")
*/
#[PHPCR\ReferenceOne(strategy: 'hard')]
private $hardTarget;
/**
* @PHPCR\ReferenceOne(strategy="path")
*/
#[PHPCR\ReferenceOne(strategy: 'path')]
private $pathTarget;
/**
* @PHPCR\ReferenceMany(strategy="weak")
*/
#[PHPCR\ReferenceMany(strategy: 'weak')]
private $weakGroup;
/**
* @PHPCR\ReferenceMany(strategy="hard")
*/
#[PHPCR\ReferenceMany(strategy: 'hard')]
private $hardGroup;
/**
* @PHPCR\ReferenceMany(strategy="path")
*/
#[PHPCR\ReferenceMany(strategy: 'path')]
private $pathGroup;
.. code-block:: xml
Expand Down Expand Up @@ -361,16 +335,12 @@ will contain the referenced document.

.. code-block:: php
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;
/**
* @PHPCR\Referrers(referringDocument="FQN\Class\Name", referencedBy="otherFieldName")
*/
#[PHPCR\Referrers(referringDocument: ClassName::class, referencedBy: 'otherFieldName')]
private $specificReferrers;
/**
* @PHPCR\Referrers(referringDocument="Other\Class\Name", referencedBy="someFieldName", cascade="persist, remove")
*/
#[PHPCR\Referrers(referringDocument: OtherClassName::class, referencedBy: 'someFieldName', cascade: 'persist, remove')]
private $cascadedReferrers;
.. code-block:: xml
Expand Down Expand Up @@ -429,16 +399,12 @@ An example for this is the `Generic` document provided by phpcr-odm itself.

.. code-block:: php
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;
/**
* @PHPCR\MixedReferrers
*/
#[PHPCR\MixedReferrers]
private $allReferrers;
/**
* @PHPCR\MixedReferrers(referenceType="hard")
*/
#[PHPCR\MixedReferrers(referenceType: 'hard')]
private $hardReferrers;
.. code-block:: xml
Expand Down Expand Up @@ -551,16 +517,12 @@ You have to be careful when using document fields that contain a
collection of related documents. Say we have a User document that
contains a collection of groups::

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

/**
* @PHPCR\Document
*/
#[PHPCR\Document]
class User
{
/**
* @PHPCR\ReferenceMany
*/
#[PHPCR\ReferenceMany]
private $groups;

public function getGroups()
Expand All @@ -579,16 +541,12 @@ This is why we recommend to initialize all collection fields to an
empty ``ArrayCollection`` in your documents constructor::

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Doctrine\ODM\PHPCR\Mapping\Attributes as PHPCR;

/**
* @PHPCR\Document
*/
#[PHPCR\Document]
class User
{
/**
* @PHPCR\ReferenceMany
*/
#[PHPCR\ReferenceMany]
private $groups;

public function __construct()
Expand Down
Loading

0 comments on commit 68f0da2

Please sign in to comment.