Skip to content

Commit d53e11e

Browse files
committed
updated PropertyAccess docs for PR 30536
1 parent 67cc8ed commit d53e11e

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
@@ -168,6 +168,30 @@ getters, this means that you can do something like this::
168168

169169
This will produce: ``He is an author``
170170

171+
Accessing a non existing property path
172+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
173+
174+
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>`
175+
does not exist.
176+
You can change this behaviour using the :method:`Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder::disableExceptionOnInvalidPropertyPath`
177+
method::
178+
179+
// ...
180+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
181+
->disableExceptionOnInvalidPropertyPath()
182+
->getPropertyAccessor();
183+
184+
class Person
185+
{
186+
public $name;
187+
}
188+
189+
$person = new Person();
190+
191+
// instead of throwing an exception the following code returns null
192+
$value = $propertyAccessor->getValue($person, 'birthday');
193+
194+
171195
Magic ``__get()`` Method
172196
~~~~~~~~~~~~~~~~~~~~~~~~
173197

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)