From d995203ee1c0fe1abe33982b5f43007df3e9aac8 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Thu, 21 Sep 2017 17:29:54 +0900 Subject: [PATCH 1/2] Promote nullable types from PHP 7.1 Also don't show type hinting in a bad light with sentences such as "If you insist on type-hinting" --- .../reference/working-with-associations.rst | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/docs/en/reference/working-with-associations.rst b/docs/en/reference/working-with-associations.rst index e115efbc9..15160a79c 100644 --- a/docs/en/reference/working-with-associations.rst +++ b/docs/en/reference/working-with-associations.rst @@ -238,14 +238,19 @@ the database permanently. Notice how both sides of the bidirectional association are always updated. Unidirectional associations are consequently simpler to -handle. Also note that if you use type-hinting in your methods, i.e. -``setAddress(Address $address)``, PHP will only allow null -values if ``null`` is set as default value. Otherwise -setAddress(null) will fail for removing the association. If you -insist on type-hinting a typical way to deal with this is to -provide a special method, like ``removeAddress()``. This can also -provide better encapsulation as it hides the internal meaning of -not having an address. +handle. + +Also note that if you use type-hinting in your methods, i.e. +``setAddress(Address $address)``, you will have to specifically +allow null values, otherwise ``setAddress(null)`` will fail to +remove the association. Starting from PHP 7.1 you should use +nullable types by prefixing the type with a ``?``, +``setAddress(?Address $address)``. Older PHP versions will only +allow null values if ``null`` is set as default value, +``setAddress(Address $address = null)``. Yet another way to deal +with this is to provide a special method, like ``removeAddress()``. +This can also provide better encapsulation as it hides the internal +meaning of not having an address. When working with collections, keep in mind that a Collection is essentially an ordered map (just like a PHP array). That is why the @@ -396,7 +401,7 @@ There are two approaches to handle this problem in your code: 1. Ignore updating the inverse side of bidirectional collections, BUT never read from them in requests that changed their state. In - the next Request Doctrine hydrates the consistent collection state + the next request Doctrine hydrates the consistent collection state again. 2. Always keep the bidirectional collections in sync through association management methods. Reads of the Collections directly From 3d3ecc77bd9f70ebabce05f34bb90860e3c98df2 Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Thu, 21 Sep 2017 21:28:19 +0900 Subject: [PATCH 2/2] Drop PHP version specifics --- docs/en/reference/working-with-associations.rst | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/docs/en/reference/working-with-associations.rst b/docs/en/reference/working-with-associations.rst index 15160a79c..de236cf40 100644 --- a/docs/en/reference/working-with-associations.rst +++ b/docs/en/reference/working-with-associations.rst @@ -240,17 +240,12 @@ Notice how both sides of the bidirectional association are always updated. Unidirectional associations are consequently simpler to handle. -Also note that if you use type-hinting in your methods, i.e. -``setAddress(Address $address)``, you will have to specifically -allow null values, otherwise ``setAddress(null)`` will fail to -remove the association. Starting from PHP 7.1 you should use -nullable types by prefixing the type with a ``?``, -``setAddress(?Address $address)``. Older PHP versions will only -allow null values if ``null`` is set as default value, -``setAddress(Address $address = null)``. Yet another way to deal -with this is to provide a special method, like ``removeAddress()``. -This can also provide better encapsulation as it hides the internal -meaning of not having an address. +Also note that if you use type-hinting in your methods, you will +have to specify a nullable type, i.e. ``setAddress(?Address $address)``, +otherwise ``setAddress(null)`` will fail to remove the association. +Another way to deal with this is to provide a special method, like +``removeAddress()``. This can also provide better encapsulation as +it hides the internal meaning of not having an address. When working with collections, keep in mind that a Collection is essentially an ordered map (just like a PHP array). That is why the