Problem/Motivation
When serializing DrupalDateTime object the following exception is thrown:
LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1574 of web/core/lib/Drupal/Core/Database/Connection.php).
This is caused by formatTranslationCache property of the object. If there is another language selected than English, this property will contain references to stringTranslation service that it its turn can have database service references if you use locale module.
Steps to reproduce
PHP version: 7.3.24
Web Server: Apache
OS: Centos 7
1. Install Drupal 8.9.x
1a. Add language other than "English".
1b. Make sure that there are translations of months, days of the weeks in the database.
2. Install Commerce module 2.21
3. Install Commerce Shipping module.
4. Use delivery_date property of ShippingRate class. I am not aware of any contrib module that does it, but my custom shipping method provides rates that depend on date. The instance of this class is added to ShippingRates widget that is displayed on checkout form.
5. Install Commerce Promotion or Commerce GiftCard module, so that it is possible to run ajax callbacks on checkout for.
6. Get to the step where you can select the shipping rate and use the language other than English.
7. Try to add coupon/card code.
8. Observe the exception described in the "Problem Motivation".
9. Change to English language, try to do the same, everything should work as expected.
I guess there are easier steps to reproduce, like creating a form with ajax callback that has DrupalDateTime object for any form element.
Proposed resolution
Implement __sleep() method in DrupalDateTime class like:
public function __sleep() {
return ['langcode', 'dateTimeObject'];
}
Remaining tasks
Write tests, make sure that no other properties of the class are needed in serialized version of the instance.
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
Not needed.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3187004
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
a.dmitriiev commentedComment #4
larowlanThanks, patch looks straightforward, just need a test
Comment #6
mohit_aghera commentedComment #7
mohit_aghera commentedComment #8
larowlanLooking good, would be good to somehow see a failing test patch
Comment #9
matroskeenI was recently dealing with serializing issues in migration tests, so I have thrown in my 2 cents.
Also, I'm +1 to upload a test-only patch and moving to NW to address remaining items.
Comment #10
matroskeenFor some reason, GitLab didn't send me a notification about your comment @mohit_rocks...
I have just one more question in gitlab ;)
I'm also wondering why the target branch is 8.9.x. Shouldn't it be 9.2.x?
Comment #11
a.dmitriiev commentedRe-rolled patch for 9.2.x
Comment #12
neclimdulCouldn't figure out what was going on in the merge request. 1000s of changes and commits so maybe it needs to rebased or something.
If I understand the problem correctly, this is just on the property caching localisation. Looking at the patch, could we avoid a lot of complexity with the trait and wakeup stuff and just empty the cache during serialization and assume it can(and should) be rebuilt after serialization? The rest shouldn't really matter.
Comment #15
a.dmitriiev commentedI created another MR to target to 9.3.x branch.
In my particular case the DrupalDateTime object was used in ajax form and the form cache was broken because of date object serialization, because localization has database service.
Comment #16
a.dmitriiev commentedComment #18
kristen polThanks for the MR and patch. If I understand correctly, the patch is for 8.9 and the MR is for 9.4.
I looked at the patch in #11 and noticed a couple minor nitpicks:
Nitpick: Ordering doesn't match function ordering (__sleep then __wakeup) but I see this is the same as
core/modules/forum/src/ForumManager.phpso I guess it should stay.Period is missing.
The MR has a gazillion changes so I'm not sure what to do there: https://git.drupalcode.org/project/drupal/-/merge_requests/112.diff
Comment #19
ranjith_kumar_k_u commentedJust fixed CS error
Comment #20
a.dmitriiev commentedKristen, the MR 122 is already closed because it is outdated. The new MR is here https://git.drupalcode.org/project/drupal/-/merge_requests/1340
Comment #21
kristen polMoving back to needs work based on feedback above.
Comment #29
recrit commentedComment #30
recrit commentedI created a new MR 6334 based on 11.x. The following have been address.
- #18 code cleanup - DONE..
- Regarding MR1340 comment "Can we replace this by just settings the property definition to protected $formatTranslationCache = [];" - DONE
- Regarding MR1340 comment "This class really doesn't need any of the service detection magic. Lets just do this and get rid of the trait." - This does need the DependencySerializationTrait for the service "string_translation" stored in protected $stringTranslation.
Comment #31
recrit commentedattaching a static patch for composer builds.
Comment #32
smustgrave commentedLeft some nitpicky stuff on the MR.
Comment #33
recrit commented@smustgrave All MR comments have been resolved
Comment #34
smustgrave commentedAll feedback appears to be addressed!
Comment #35
larowlanIssue credits
Comment #39
larowlanCommitted to 11.x and backported to 10.2.x - thanks!