-
-
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
Use typed properties for default metadata #2411
Conversation
Do you think we could also add support for References and embedded documents there? |
5cb2d81
to
cc02d38
Compare
Right now it sets |
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.
Nice, I think I missed that during review 👍🏻 This looks great and good to go from my side 🚀
I'm just wondering if we need to add the changelog
entry, as it could potentially introduce a BC break for people who relied on non-strict PHP type casting:
#[Field] // Will be stored in DB as `string` but casted back to `int`
private int $myProp;
which doesn't seem like a big deal though.
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.
Awesome idea and job porting the feature from ORM! Two additional ideas:
- Do you know why ORM is not inferring nullable typed properties? I think we should do that, seems pretty straightforward, but I wonder if there are any dragons down the road.
- In the same vein as EmbedOne we could autocomplete
collectionClass
attribute for EmbedMany/ReferenceMany. I can take care of it in a follow-up PR if you'd like me to
docs/en/reference/basic-mapping.rst
Outdated
- ``bool``: ``bool`` | ||
- ``float``: ``float`` | ||
- ``int``: ``int`` | ||
- ``string`` or any other type: ``string`` |
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.
- ``string`` or any other type: ``string`` | |
- ``string``: ``string`` |
I'd remove the "or any other type" part as this paragraph is before we teach user what types we have. Now I wrote that I think the section should be below listing all available mapping types :)
{ | ||
$type = $this->reflClass->getProperty($mapping['fieldName'])->getType(); | ||
|
||
if ($type instanceof ReflectionNamedType && ! isset($mapping['type'])) { |
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'd be better to have an early return instead of a nested switch.
|
||
// float | ||
$cm->mapField(['fieldName' => 'float']); | ||
self::assertEquals(Type::FLOAT, $cm->getTypeOfField('float')); |
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.
int
seems to be missing compared to the list of what is autocompleted :)
I'll take a look at those maybe later today, with the nullable type I remember there were some BC breaks as doctrine/orm#8723, but I haven't checked the reason. |
Interesting! I'll add a comment for that case |
cc02d38
to
5d29ffd
Compare
About the nullable type property see doctrine/orm#8723 (comment) I'm not sure if that would introduce some unwanted behaviour 🤔 |
8afcb4d
to
90600fb
Compare
Regarding nullable properties, I think we should leave it in current state. The behaviour in ODM is slightly different - |
493e765
to
c04baac
Compare
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.
As for nullable types I'd suggest to leave this PR as is and merge it once checks are passing. Then create a follow-up PR that will emit a deprecation once it detect typed nullable property that is not on par with mapping with intention to throw an exception in 3.0.
UPGRADE-2.4.md
Outdated
private int $myProp; | ||
``` | ||
|
||
This property will be stored in DB as `string` but casted back to `int`. |
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 property will be stored in DB as `string` but casted back to `int`. | |
This property will be stored in DB as `string` but casted back to `int`. Please note that at this | |
time, due to backward compatibility reasons, nullable type does not imply `nullable` mapping. |
- ``float``: ``float`` | ||
- ``int``: ``int`` | ||
- ``string``: ``string`` | ||
|
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'd add similar note about nullable
as in upgrade docs as the behaviour is not fully intuitional
c04baac
to
7276a92
Compare
tests/Documents74/UserTyped.php
Outdated
#[ODM\ReferenceMany] | ||
public CustomCollection $referenceMany; | ||
|
||
public static function loadMetadata(ClassMetadata $metadata): void |
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.
Just noticed: what's this?
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 was copied from the ORM thinking about us having support for Static PHP Mapping, but we are not, so I'll remove it, thanks!
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.
Are you fixing the PHPStan failure here or in a follow-up PR as it seems unrelated?
I was creating #2417 👍 to target |
23ee469
to
bfa8161
Compare
bfa8161
to
ee0f876
Compare
Thanks @franmomu 🚀 |
About the deprecation thing: https://doctrine.slack.com/archives/CAAE6T66B/p1647934766611479?thread_ts=1647885533.164609&cid=CAAE6T66B
|
Summary
I'll retarget to2.4.x
once is created.This is kind of based on doctrine/orm#8439 and doctrine/orm#8589.
The idea is that if you use typed properties, it gets the type from that properties. For
array
, I've chosenhash
, I thought it was more right.