Skip to content

Commit c95da92

Browse files
committed
feature #11156 updated PropertyAccess docs for PR 30536 (wuestkamp)
This PR was merged into the master branch. Discussion ---------- updated PropertyAccess docs for PR 30536 PR: symfony/symfony#30545 Issue: symfony/symfony#30536 Commits ------- d53e11e updated PropertyAccess docs for PR 30536
2 parents c316550 + d53e11e commit c95da92

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

components/property_access.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,30 @@ getters, this means that you can do something like this::
166166

167167
This will produce: ``He is an author``
168168

169+
Accessing a non existing property path
170+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171+
172+
By default a :class:`Symfony\\Component\\PropertyAccess\\Exception\\NoSuchPropertyException` is thrown if the property path passed to :method:`PropertyAccessor::getValue<Symfony\\Component\\PropertyAccess\\PropertyAccessor::getValue>`
173+
does not exist.
174+
You can change this behaviour using the :method:`Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder::disableExceptionOnInvalidPropertyPath`
175+
method::
176+
177+
// ...
178+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
179+
->disableExceptionOnInvalidPropertyPath()
180+
->getPropertyAccessor();
181+
182+
class Person
183+
{
184+
public $name;
185+
}
186+
187+
$person = new Person();
188+
189+
// instead of throwing an exception the following code returns null
190+
$value = $propertyAccessor->getValue($person, 'birthday');
191+
192+
169193
Magic ``__get()`` Method
170194
~~~~~~~~~~~~~~~~~~~~~~~~
171195

reference/configuration/framework.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ Configuration
107107

108108
* `magic_call`_
109109
* `throw_exception_on_invalid_index`_
110+
* `throw_exception_on_invalid_property_path`_
110111

111112
* `property_info`_
112113

@@ -1726,6 +1727,18 @@ throw_exception_on_invalid_index
17261727
When enabled, the ``property_accessor`` service throws an exception when you
17271728
try to access an invalid index of an array.
17281729

1730+
throw_exception_on_invalid_property_path
1731+
........................................
1732+
1733+
**type**: ``boolean`` **default**: ``true``
1734+
1735+
.. versionadded:: 4.3
1736+
1737+
The ``throw_exception_on_invalid_property_path`` option was introduced in Symfony 4.3.
1738+
1739+
When enabled, the ``property_accessor`` service throws an exception when you
1740+
try to access an invalid property path of an object.
1741+
17291742
property_info
17301743
~~~~~~~~~~~~~
17311744

0 commit comments

Comments
 (0)