-
-
Notifications
You must be signed in to change notification settings - Fork 507
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
Conversation
@@ -281,7 +281,7 @@ Optional attributes: | |||
], | |||
defaultDiscriminatorValue: 'user', | |||
)] | |||
private $creator; | |||
private User|Author $creator; |
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.
@@ -417,7 +417,7 @@ to be considered when looking up one-to-one relationships: | |||
cascade: 'all', | |||
storeAs: 'id', | |||
)] | |||
private $items; | |||
private Collection $items; |
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 is a ReferenceOne
. The variable is plural so I supposed it's a Collection
. But I would have expected a single Item
. I'll check.
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 should be:
private Collection $items; | |
private Item $item; |
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. |
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.
PersistentCollection
is not internal, so I think we can mention this class.
@@ -85,11 +85,11 @@ follows: | |||
{ | |||
// ... | |||
|
|||
private $_listeners = []; |
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.
Prefixing private properties with an underscore is so PHP 4.
@@ -31,7 +31,7 @@ class: | |||
public function convertToPHPValue($value): \DateTime | |||
{ | |||
// This is called to convert a Mongo value to a PHP representation | |||
return new \DateTime('@' . $value->sec); | |||
return $value->toDateTime(); |
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.
It's so much simpler.
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.
Unrelated, but we should try to come up with a different example, as this only reproduces the DateTime
type. I've previously used the example of storing the time zone alongside the UTCDateTime
instance and applying that to the date type when reading.
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.
Done: #2654
} | ||
|
||
.. note:: |
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.
Good to have this documented 👍
@@ -281,7 +281,7 @@ Optional attributes: | |||
], | |||
defaultDiscriminatorValue: 'user', | |||
)] | |||
private $creator; | |||
private User|Author $creator; |
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.
@@ -417,7 +417,7 @@ to be considered when looking up one-to-one relationships: | |||
cascade: 'all', | |||
storeAs: 'id', | |||
)] | |||
private $items; | |||
private Collection $items; |
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 should be:
private Collection $items; | |
private Item $item; |
@@ -31,7 +31,7 @@ class: | |||
public function convertToPHPValue($value): \DateTime | |||
{ | |||
// This is called to convert a Mongo value to a PHP representation | |||
return new \DateTime('@' . $value->sec); | |||
return $value->toDateTime(); |
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.
Unrelated, but we should try to come up with a different example, as this only reproduces the DateTime
type. I've previously used the example of storing the time zone alongside the UTCDateTime
instance and applying that to the date type when reading.
|
||
#[Field(notSaved: true)] | ||
public $score; | ||
public int $score; |
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.
The type of the field defaults to string, but I agree that the property name indicates an integer. With that in mind, we should add type: 'int'
to the field attribute.
#[ReferenceMany(targetDocument: Account::class)] | ||
private $accounts = []; | ||
private Collection $accounts; |
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 may apply to multiple classes, but we should default to initialising this with an empty ArrayCollection
instance so it doesn't have to happen in the constructor:
private Collection $accounts; | |
private Collection $accounts = new ArrayCollection(); |
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.
That's not allowed unless CPP is used. Adding the constructor.
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.
Yep, this is still PHP 😁
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.
OMFG - I could've sworn that you can initialise properties like that in general now. Boo :(
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.
One minor change to namespaces, but LGTM aside from that
#[ReferenceMany(targetDocument: Account::class)] | ||
private $accounts = []; | ||
private Collection $accounts; |
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.
OMFG - I could've sworn that you can initialise properties like that in general now. Boo :(
Co-authored-by: Andreas Braun <git@alcaeus.org>
* 2.9.x: (24 commits) Fix typo in code example (#2670) Label PRs about GH actions with "CI" (#2632) Review basic mapping (#2668) Fix wording (#2667) Add native type to private properties and final classes (#2666) Review and add tests on `ResolveTargetDocumentListener` (#2660) Remove soft-delete-cookbook (#2657) doc: Remove wakeup and clone cookbook (#2663) Modernize generated code for Hydrators (#2665) Add tests for introduction (#2664) doc: Review mapping ORM and ODM cookbook (#2658) doc: Review cookbook on blending ORM and ODM (#2656) doc: Review and test validation cookbook (#2662) Update custom mapping example (#2654) doc: Review Simple Search Engine Cookbook (#2659) doc: Add cookbook about embedding referenced documents using $lookup (#2655) doc: Add type to properties (#2652) doc: Review custom collections and repository docs (#2653) doc: Review Getting Started (#2650) Move annotations-reference to attributes-reference (#2651) ...
Summary
Using types properties is recommended to improve type safety.
I still have to check that everything works properly if the properties are not set with a default value. Because when there's no type, the value is
null
by default.