I have a Symfony Command that reads rows from a spreadsheet and inserts them into a MySQL table. The command was working fine in Symfony 4.4. I started getting this error after I upgraded my version of Symfony and PHP.
DoctrineDBALTypesExceptionInvalidType: Could not convert PHP value of type DateTimeImmutable to type DoctrineDBALTypesDateTimeType. Expected one of the following types: null, DateTime.
This is the code that seems to be throwing the error
$accountstatement->setAccountStatementCreatedOn(new DateTimeImmutable());
$em->persist($accountstatement);
$em->flush();
My Stack versions:
Symfony : 6.4.9
PHP : 8.1.2-1ubuntu2.18
doctrine/dbal 4.0.4
doctrine/deprecations 1.1.3
doctrine/doctrine-bundle 2.12.0
doctrine/doctrine-fixtures-bundle 3.6.1
doctrine/doctrine-migrations-bundle 3.3.1
doctrine/event-manager 2.0.1
doctrine/inflector 2.0.10
doctrine/instantiator 2.0.0
doctrine/lexer 3.0.1
doctrine/migrations 3.8.0
doctrine/orm 3.2.1
mysql Ver 15.1 Distrib 10.6.18-MariaDB
There are a few questions on Stackoverflow with the similar issue but the root causes in those cases are different and hence doesn’t resolve my issue. Wonder if anyone can point out what could be wrong.