1
0
mirror of synced 2025-02-02 21:41:45 +03:00

Merge pull request #7471 from alcaeus/fix-unloaded-metadata-parameter-processing

Fix parameter value processing for objects with unloaded metadata
This commit is contained in:
Luís Cobucci 2018-11-15 11:34:31 +01:00 committed by GitHub
commit d21305378c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 5 deletions

View File

@ -19,10 +19,12 @@
namespace Doctrine\ORM; namespace Doctrine\ORM;
use Doctrine\Common\Persistence\Mapping\MappingException;
use Doctrine\Common\Util\ClassUtils; use Doctrine\Common\Util\ClassUtils;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping\MappingException as ORMMappingException;
use Doctrine\ORM\Query\Parameter; use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\Cache\QueryCacheKey; use Doctrine\ORM\Cache\QueryCacheKey;
use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Cache\QueryCacheProfile;
@ -410,16 +412,24 @@ abstract class AbstractQuery
return $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); $value = $this->_em->getUnitOfWork()->getSingleIdentifierValue($value);
if ($value === null) { if ($value === null) {
throw ORMInvalidArgumentException::invalidIdentifierBindingEntity(); throw ORMInvalidArgumentException::invalidIdentifierBindingEntity();
} }
} } catch (MappingException | ORMMappingException $e) {
// Silence any mapping exceptions. These can occur if the object in
if ($value instanceof Mapping\ClassMetadata) { // question is not a mapped entity, in which case we just don't do
return $value->name; // any preparation on the value.
} }
return $value; return $value;

View File

@ -189,6 +189,25 @@ class QueryTest extends OrmTestCase
); );
} }
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() public function testDefaultQueryHints()
{ {
$config = $this->_em->getConfiguration(); $config = $this->_em->getConfiguration();