From 0552749059ad24d657ef4a0f1dfe437f58022ca2 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Tue, 13 Nov 2018 09:08:46 +0100 Subject: [PATCH] Fix parameter value processing for objects with unloaded metadata --- lib/Doctrine/ORM/AbstractQuery.php | 20 +++++++++++++++----- tests/Doctrine/Tests/ORM/Query/QueryTest.php | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index 3ba0dee263..0e66e147ff 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -19,10 +19,12 @@ namespace Doctrine\ORM; +use Doctrine\Common\Persistence\Mapping\MappingException; use Doctrine\Common\Util\ClassUtils; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\ORM\Mapping\MappingException as ORMMappingException; use Doctrine\ORM\Query\Parameter; use Doctrine\ORM\Cache\QueryCacheKey; use Doctrine\DBAL\Cache\QueryCacheProfile; @@ -410,16 +412,24 @@ public function processParameterValue($value) return $value; } - if (is_object($value) && $this->_em->getMetadataFactory()->hasMetadataFor(ClassUtils::getClass($value))) { + if ($value instanceof Mapping\ClassMetadata) { + return $value->name; + } + + if (! is_object($value)) { + return $value; + } + + try { $value = $this->_em->getUnitOfWork()->getSingleIdentifierValue($value); if ($value === null) { throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); } - } - - if ($value instanceof Mapping\ClassMetadata) { - return $value->name; + } catch (MappingException | ORMMappingException $e) { + // Silence any mapping exceptions. These can occur if the object in + // question is not a mapped entity, in which case we just don't do + // any preparation on the value. } return $value; diff --git a/tests/Doctrine/Tests/ORM/Query/QueryTest.php b/tests/Doctrine/Tests/ORM/Query/QueryTest.php index 8cceb6c503..8a3caf66f0 100644 --- a/tests/Doctrine/Tests/ORM/Query/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Query/QueryTest.php @@ -189,6 +189,25 @@ public function testProcessParameterValueClassMetadata() ); } + public function testProcessParameterValueObject() : void + { + $query = $this->_em->createQuery('SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.user = :user'); + $user = new CmsUser(); + $user->id = 12345; + + self::assertSame( + 12345, + $query->processParameterValue($user) + ); + } + + public function testProcessParameterValueNull() : void + { + $query = $this->_em->createQuery('SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.user = :user'); + + self::assertNull($query->processParameterValue(null)); + } + public function testDefaultQueryHints() { $config = $this->_em->getConfiguration();