-
-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: Add type to properties #2652
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ does not exist. | |
{ | ||
#[Field(type: 'string')] | ||
#[AlsoLoad('name')] | ||
public $fullName; | ||
public string $fullName; | ||
} | ||
|
||
The ``$fullName`` property will be loaded from ``fullName`` if it exists, but | ||
|
@@ -217,6 +217,7 @@ Optional attributes: | |
|
||
class User | ||
{ | ||
/** @var Collection<BookTag|SongTag> */ | ||
#[EmbedMany( | ||
strategy:'set', | ||
discriminatorField:'type', | ||
|
@@ -226,7 +227,7 @@ Optional attributes: | |
], | ||
defaultDiscriminatorValue: 'book', | ||
)] | ||
private $tags = []; | ||
private Collection $tags; | ||
} | ||
|
||
Depending on the embedded document's class, a value of ``user`` or ``author`` | ||
|
@@ -281,7 +282,7 @@ Optional attributes: | |
], | ||
defaultDiscriminatorValue: 'user', | ||
)] | ||
private $creator; | ||
private User|Author $creator; | ||
} | ||
|
||
Depending on the embedded document's class, a value of ``user`` or ``author`` | ||
|
@@ -304,7 +305,7 @@ relationship. | |
class Money | ||
{ | ||
#[Field(type: 'float')] | ||
private $amount; | ||
private float $amount; | ||
|
||
public function __construct(float $amount) | ||
{ | ||
|
@@ -317,7 +318,7 @@ relationship. | |
class Wallet | ||
{ | ||
#[EmbedOne(targetDocument: Money::class)] | ||
private $money; | ||
private Money $money; | ||
|
||
public function setMoney(Money $money): void | ||
{ | ||
|
@@ -374,13 +375,13 @@ Examples: | |
class User | ||
{ | ||
#[Field(type: 'string')] | ||
protected $username; | ||
protected string $username; | ||
|
||
#[Field(type: 'string', name: 'co')] | ||
protected $country; | ||
protected string $country; | ||
|
||
#[Field(type: 'float')] | ||
protected $height; | ||
protected float $height; | ||
} | ||
|
||
.. _file: | ||
|
@@ -501,9 +502,16 @@ customize this via the :ref:`strategy <basic_mapping_identifiers>` attribute. | |
class User | ||
{ | ||
#[Id] | ||
protected $id; | ||
protected string $id; | ||
} | ||
|
||
.. note:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to have this documented 👍 |
||
|
||
The property annotated with `#[Id]`_ cannot be ``readonly`` even if the | ||
value is set only once and should never be updated. This is because the | ||
property is set outside of the scope of the class, which is not allowed in | ||
PHP for ``readonly`` properties. | ||
|
||
#[Index] | ||
-------- | ||
|
||
|
@@ -551,7 +559,7 @@ If you are creating a single-field index, you can simply specify an `#[Index]`_ | |
{ | ||
#[Field(type: 'string')] | ||
#[UniqueIndex] | ||
private $username; | ||
private string $username; | ||
} | ||
|
||
.. note:: | ||
|
@@ -631,7 +639,7 @@ This is only compatible with the ``int`` type, and cannot be combined with `#[Id | |
{ | ||
#[Field(type: 'int')] | ||
#[Lock] | ||
private $lock; | ||
private int $lock; | ||
} | ||
|
||
#[MappedSuperclass] | ||
|
@@ -979,19 +987,20 @@ Optional attributes: | |
|
||
class User | ||
{ | ||
/** @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', | ||
)] | ||
private $cart; | ||
private Collection $cart; | ||
} | ||
|
||
.. _attributes_reference_reference_one: | ||
|
@@ -1061,7 +1070,7 @@ Optional attributes: | |
], | ||
defaultDiscriminatorValue: 'book' | ||
)] | ||
private $cart; | ||
private Item $cart; | ||
} | ||
|
||
#[SearchIndex] | ||
|
@@ -1148,7 +1157,7 @@ Alias of `#[Index]`_, with the ``unique`` option set by default. | |
{ | ||
#[Field(type: 'string')] | ||
#[UniqueIndex] | ||
private $email; | ||
private string $email; | ||
} | ||
|
||
.. _attributes_reference_version: | ||
|
@@ -1258,7 +1267,7 @@ combined with `#[Id]`_. Following ODM types can be used for versioning: ``int``, | |
{ | ||
#[Field(type: 'int')] | ||
#[Version] | ||
private $version; | ||
private int $version; | ||
} | ||
|
||
By default, Doctrine ODM updates :ref:`embed-many <embed_many>` and | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,19 +56,23 @@ Example: | |
namespace MyProject\Model; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
|
||
class User | ||
{ | ||
|
||
/** @var ArrayCollection */ | ||
private $addresses; | ||
|
||
/** @var ArrayCollection */ | ||
private $articles; | ||
private Collection $addresses; | ||
private Collection $articles; | ||
|
||
public function __construct() | ||
{ | ||
$this->addresses = new ArrayCollection(); | ||
$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. | ||
Comment on lines
+75
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,11 +85,12 @@ follows: | |
{ | ||
// ... | ||
|
||
private $_listeners = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefixing private properties with an underscore is so PHP 4. |
||
/** @var PropertyChangedListener[] */ | ||
private array $listeners = []; | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function addPropertyChangedListener(PropertyChangedListener $listener): void | ||
{ | ||
$this->_listeners[] = $listener; | ||
$this->listeners[] = $listener; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope I didn't make a mistake. I find that the type helps to understand the content of the variable here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks correct to me based on the discriminator map.