Skip to content

Commit

Permalink
Merge pull request #7471 from alcaeus/fix-unloaded-metadata-parameter…
Browse files Browse the repository at this point in the history
…-processing

Fix parameter value processing for objects with unloaded metadata
  • Loading branch information
lcobucci authored Nov 15, 2018
2 parents 5208035 + 0552749 commit d213053
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/Doctrine/ORM/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d213053

Please sign in to comment.