-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[PropertyAccess] Add nullsafe operator support #34483
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
Comments
I like the idea, started to work on something, probably won't have time to spend more time on it before Sunday. |
non news on this feature ? |
Well, nobody finished the work of implementing it |
Thank you for this suggestion. |
I'd still like to see this happen, but someone needs to take over #34497 to introduce the nullsafe operator support in the PropertyAccess component. |
…s (fsoedjede) This PR was merged into the 6.2 branch. Discussion ---------- [Form][PropertyAccess] Allow optional property accesses | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | yes <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #34483 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#17288 <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> This PR is basically a copy of #34497 with some minor changes. I needed it and I have some time now to help advance in implementing it. **TODO** - [x] Add nullsafe operator usage - [x] Update unit test - [x] Update documentation Regards Commits ------- 6455feb feature #34483 - Allow optional property accesses
Description
Given an object graph
foo.bar.baz
, wherebar
is optional (can be null),trying to call the property accessor with
foo.bar.baz
will throw a:In some use-cases, it'll be very handy to get
null
instead of this exception (e.g: in custom validation constraints using a property path set by the developper in annotations).The flag introduced in #30545 allows to ignore exception on invalid property paths, but it works differently and has some drawbacks:
foo.bak
(non-existing property) will be considered as readable and return null. Butfoo.bar.baz
would still throw the same exception.bar
must exist, but can benull
).Then I was about to suggest yet another flag for the
PropertyAccessor
($throwOnNullValueInGraph
?), but instead, I'm wondering if we shouldn't allow something mimicking the PHP nullsafe_calls RFC 👇Example
Given an object graph
foo.bar.baz
, wherebar
is optional (can be null):This wouldn't change anything for writing, only for reading.
The text was updated successfully, but these errors were encountered: