Skip to content

Commit

Permalink
Add phpdoc for Collection generics
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jun 24, 2024
1 parent 77b525e commit 688a9d1
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 9 deletions.
2 changes: 2 additions & 0 deletions docs/en/reference/aggregation-stage-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ pipeline stages. Take the following relationship for example:
class Orders
{
/** @var Collection<Item> */
#[ReferenceMany(
targetDocument: Item::class,
cascade: 'all',
Expand Down Expand Up @@ -412,6 +413,7 @@ to be considered when looking up one-to-one relationships:
class Orders
{
/** @var Collection<Item> */
#[ReferenceOne(
targetDocument: Item::class,
cascade: 'all',
Expand Down
7 changes: 4 additions & 3 deletions docs/en/reference/attributes-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ Optional attributes:
class User
{
/** @var Collection<BookTag|SongTag> */
#[EmbedMany(
strategy:'set',
discriminatorField:'type',
Expand Down Expand Up @@ -989,13 +990,13 @@ Optional attributes:
/** @var Collection<Item> */
#[ReferenceMany(
strategy: 'set',
targetDocument: Documents\Item::class,
targetDocument: Item::class,
cascade: 'all',
sort: ['sort_field' => 'asc'],
discriminatorField: 'type',
discriminatorMap: [
'book' => Documents\BookItem::class,
'song' => Documents\SongItem::class,
'book' => BookItem::class,
'song' => SongItem::class,
],
defaultDiscriminatorValue: 'book',
)]
Expand Down
7 changes: 7 additions & 0 deletions docs/en/reference/best-practices.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ Example:
$this->articles = new ArrayCollection();
}
}
.. note::

The properties' type hints must be ``Collection``, and cannot be
``ArrayCollection``. When the ``User`` object is retrieved from the database,
the properties ``$addresses`` and ``$articles`` are instances of
``Doctrine\ODM\MongoDB\PersistentCollection`` to track changes.
5 changes: 4 additions & 1 deletion docs/en/reference/embedded-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Embed many documents:
{
// ...
/** @var Collection<PhoneNumber>
/** @var Collection<PhoneNumber> */
#[EmbedMany(targetDocument: Phonenumber::class)]
private Collection $phoneNumbers;
Expand Down Expand Up @@ -282,6 +282,8 @@ You can achieve this behavior by using the `storeEmptyArray` option for embedded
class User
{
// ...
/** @var Collection<PhoneNumber> */
#[EmbedMany(targetDocument: PhoneNumber::class, storeEmptyArray: true)]
private Collection $phoneNumbers;
// ...
Expand All @@ -299,5 +301,6 @@ You can achieve this behavior by using the `storeEmptyArray` option for embedded
<field name="number" type="string" />
</embedded-document>
</doctrine-mongo-mapping>
Now, when the `$phoneNumbers` collection is empty, an empty array will be stored in the database for the `User`
document's embedded `phoneNumbers` collection, even if there are no actual embedded documents in the collection.
1 change: 1 addition & 0 deletions docs/en/reference/indexes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Now if we had a ``BlogPost`` document with the ``Comment`` document embedded man
#[Index]
private string $slug;
/** @var Collection<Comment> */
#[EmbedMany(targetDocument: Comment::class)]
private Collection $comments;
}
Expand Down
2 changes: 2 additions & 0 deletions docs/en/reference/priming-references.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Consider the following abbreviated model:
#[Document]
class User
{
/** @var Collection<Account> */
#[ReferenceMany(targetDocument: Account::class)]
private Collection $accounts;
}
Expand Down Expand Up @@ -111,6 +112,7 @@ specifying them in the mapping:
#[Document]
class User
{
/** @var Collection<Account> */
#[ReferenceMany(targetDocument: Account::class, prime: ['user'])]
private Collection $accounts;
}
Expand Down
20 changes: 15 additions & 5 deletions docs/en/reference/reference-mapping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ in each `DBRef`_ object:
{
// ..
/** @var Collection<Album|Song> */
#[ReferenceMany(
discriminatorMap: [
'album' => Album::class,
Expand Down Expand Up @@ -277,8 +278,11 @@ Example:
<?php
#[ReferenceOne(targetDocument: Profile::class, storeAs: 'id')]
private Profile $profile;
class User
{
#[ReferenceOne(targetDocument: Profile::class, storeAs: 'id')]
private Profile $profile;
}
.. code-block:: xml
Expand Down Expand Up @@ -323,8 +327,11 @@ referenced documents. You must explicitly enable this functionality:
<?php
#[ReferenceOne(targetDocument: Profile::class, cascade: ['persist'])]
private Profile $profile;
class User
{
#[ReferenceOne(targetDocument: Profile::class, cascade: ['persist'])]
private Profile $profile;
}
.. code-block:: xml
Expand Down Expand Up @@ -435,13 +442,16 @@ You can achieve this behavior by using the `storeEmptyArray` option.

.. code-block:: php
<?php
#[Document]
class User
{
// ...
/** @var Collection<Account> */
#[ReferenceMany(targetDocument: Account::class, storeEmptyArray: true)]
private Collection $accounts = [];
private Collection $accounts;
// ...
}
.. code-block:: xml
Expand Down
5 changes: 5 additions & 0 deletions docs/en/reference/trees.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Full Tree in Single Document
#[Field(type: 'string')]
private string $body;
/** @var Collection<Comment> */
#[EmbedMany(targetDocument: Comment::class)]
private Collection $comments;
Expand All @@ -38,6 +39,7 @@ Full Tree in Single Document
#[Field(type: 'string')]
private string $text;
/** @var Collection<Comment> */
#[EmbedMany(targetDocument: Comment::class)]
private Collection $replies;
Expand Down Expand Up @@ -113,6 +115,7 @@ Child Reference
#[Field(type: 'string')]
private string $name;
/** @var Collection<Category> */
#[ReferenceMany(targetDocument: Category::class)]
#[Index]
private Collection $children;
Expand Down Expand Up @@ -169,10 +172,12 @@ Array of Ancestors
#[Id]
private string $id;
/** @var Collection<Category> */
#[ReferenceMany(targetDocument: Category::class)]
#[Index]
private Collection $ancestors;
/** @var Collection<Category> */
#[ReferenceOne(targetDocument: Category::class)]
#[Index]
private ?Category $parent = null;
Expand Down

0 comments on commit 688a9d1

Please sign in to comment.