This is a followup of doctrine/dbal#3838, I just want to check if maintainers see a room for improvement.
TL;DR: when we check metadata for doctrine entities, there is a cache that prevents checking for metadata across requests and there is memoization to prevent hitting on the cache. When passing objects that are not doctrine entities with fallback there is no cache but memoization, and without fallback there is none of that.
There are at least two paths that trigger that behavior (just to show that this is not an easy fix in userland):
1/ doctrine query with datetime parameter without the third parameter "datetime" (dev and prod)
$q = $em->createQuery('SELECT d from App\Entity\DataSample d WHERE d.referenceDate = :date');
$q->setParameter('date', Carbon::now(), 'datetime');
This is fixable in userland, but quite not fun.
This is a blackfire profile in prod env with 20 sql requests with a datetime parameter WITHOUT the third parameter set. The after is a simple test at the beginning of the getMetadataFor to avoid loading the metadata stack:
if ($className === 'DateTime') {
throw new MappingException();
}

2/ symfony validation (dev only)
3/ doctrine embedable classes
I don't know the best fix for that but I figures that on the life on a request / cache, this should not happen. Even I who know this bug, I forgot to add the third parameter, it should really be resolved in doctrine/persistence.
We could:
- cache the exception and add some instanceof / throw somewhere.
- cache null and return an exception if null, we should change the isset by array_key_exists
This is a followup of doctrine/dbal#3838, I just want to check if maintainers see a room for improvement.
TL;DR: when we check metadata for doctrine entities, there is a cache that prevents checking for metadata across requests and there is memoization to prevent hitting on the cache. When passing objects that are not doctrine entities with fallback there is no cache but memoization, and without fallback there is none of that.
There are at least two paths that trigger that behavior (just to show that this is not an easy fix in userland):
1/ doctrine query with datetime parameter without the third parameter "datetime" (dev and prod)
persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php
Line 161 in fdaaa50
persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php
Line 206 in fdaaa50
This is fixable in userland, but quite not fun.
This is a blackfire profile in prod env with 20 sql requests with a datetime parameter WITHOUT the third parameter set. The after is a simple test at the beginning of the getMetadataFor to avoid loading the metadata stack:
2/ symfony validation (dev only)
persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php
Line 161 in fdaaa50
persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php
Line 206 in fdaaa50
3/ doctrine embedable classes
I don't know the best fix for that but I figures that on the life on a request / cache, this should not happen. Even I who know this bug, I forgot to add the third parameter, it should really be resolved in doctrine/persistence.
We could: