Skip to content

Commit 08684f0

Browse files
Merge #329
329: Feat: Add doctrine/orm v3 compatibility r=norkunas a=bpolaszek Add doctrine/orm: ^3.0 compatibility. Co-authored-by: Beno!t POLASZEK <[email protected]>
2 parents e080a50 + cc9225d commit 08684f0

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
},
3030
"require-dev": {
3131
"doctrine/annotations": "^2.0",
32-
"doctrine/orm": "^2.9",
32+
"doctrine/orm": "^2.9|^3.0",
3333
"matthiasnoback/symfony-dependency-injection-test": "^4.3 || ^5.0",
3434
"nyholm/psr7": "^1.5.1",
3535
"php-cs-fixer/shim": "^3.14",

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ parameters:
55
paths:
66
- src
77
- tests
8+
ignoreErrors:
9+
- '#Call to static method getClass\(\) on an unknown class Doctrine\\Common\\Util\\ClassUtils#'

src/Services/MeilisearchService.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Meilisearch\Bundle\Services;
66

77
use Doctrine\Common\Util\ClassUtils;
8+
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
89
use Doctrine\Persistence\ObjectManager;
910
use Meilisearch\Bundle\Collection;
1011
use Meilisearch\Bundle\Engine;
@@ -236,7 +237,7 @@ private function getBaseClassName($objectOrClass): string
236237
}
237238

238239
if (is_object($objectOrClass)) {
239-
return ClassUtils::getClass($objectOrClass);
240+
return self::resolveClass($objectOrClass);
240241
}
241242

242243
return $objectOrClass;
@@ -306,7 +307,7 @@ private function getAggregatorsFromEntities(ObjectManager $objectManager, array
306307
$aggregators = [];
307308

308309
foreach ($entities as $entity) {
309-
$entityClassName = ClassUtils::getClass($entity);
310+
$entityClassName = self::resolveClass($entity);
310311
if (array_key_exists($entityClassName, $this->entitiesAggregators)) {
311312
foreach ($this->entitiesAggregators[$entityClassName] as $aggregator) {
312313
$aggregators[] = new $aggregator(
@@ -367,4 +368,21 @@ private function assertIsSearchable(string $className): void
367368
throw new Exception('Class '.$className.' is not searchable.');
368369
}
369370
}
371+
372+
private static function resolveClass(object $object): string
373+
{
374+
static $resolver;
375+
376+
$resolver ??= (function () {
377+
// Doctrine ORM v3+ compatibility
378+
if (\class_exists(DefaultProxyClassNameResolver::class)) {
379+
return fn (object $object) => DefaultProxyClassNameResolver::getClass($object);
380+
}
381+
382+
// Legacy Doctrine ORM compatibility
383+
return fn (object $object) => ClassUtils::getClass($object); // @codeCoverageIgnore
384+
})();
385+
386+
return $resolver($object);
387+
}
370388
}

tests/Dbal/Type/DummyObjectIdType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?DummyObj
2828
}
2929

3030
if (!\is_string($value) && !is_int($value)) {
31-
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', 'int', DummyObjectId::class]);
31+
$actualType = \get_debug_type($value);
32+
$possibleTypes = ['null', 'string', 'int', self::class];
33+
throw new ConversionException(\sprintf("Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s", $value, $actualType, $this->getName(), \implode(', ', $possibleTypes)));
3234
}
3335

3436
return new DummyObjectId((int) $value);
@@ -48,7 +50,9 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
4850
}
4951

5052
if (!\is_string($value) && !is_int($value)) {
51-
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', 'int', DummyObjectId::class]);
53+
$actualType = \get_debug_type($value);
54+
$possibleTypes = ['null', 'string', 'int', self::class];
55+
throw new ConversionException(\sprintf("Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s", $value, $actualType, $this->getName(), \implode(', ', $possibleTypes)));
5256
}
5357

5458
return (int) $value;

0 commit comments

Comments
 (0)