Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent UnitOfWork lookup for DBAL types specified in Doctrine\ORM\Query#setParameter() #7528

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ abstract class AbstractQuery
/**
* The parameter map of this query.
*
* @var \Doctrine\Common\Collections\ArrayCollection
* @var ArrayCollection|Parameter[]
*/
protected $parameters;

Expand Down Expand Up @@ -306,7 +306,7 @@ public function free()
/**
* Get all defined parameters.
*
* @return \Doctrine\Common\Collections\ArrayCollection The defined query parameters.
* @return ArrayCollection The defined query parameters.
*/
public function getParameters()
{
Expand Down Expand Up @@ -336,7 +336,7 @@ function (Query\Parameter $parameter) use ($key) : bool {
/**
* Sets a collection of query parameters.
*
* @param \Doctrine\Common\Collections\ArrayCollection|array $parameters
* @param ArrayCollection|mixed[] $parameters
*
* @return static This query instance.
*/
Expand Down
58 changes: 40 additions & 18 deletions lib/Doctrine/ORM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

namespace Doctrine\ORM;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Exec\AbstractSqlExecutor;
use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\Query\ParameterTypeInferer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\ParserResult;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\ParameterTypeInferer;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Utility\HierarchyDiscriminatorResolver;
use function array_keys;
use function assert;

/**
* A Query object represents a DQL query.
Expand Down Expand Up @@ -387,26 +390,13 @@ private function processParameterMappings($paramMappings)
$types = [];

foreach ($this->parameters as $parameter) {
$key = $parameter->getName();
$value = $parameter->getValue();
$rsm = $this->getResultSetMapping();
$key = $parameter->getName();

if ( ! isset($paramMappings[$key])) {
throw QueryException::unknownParameter($key);
}

if (isset($rsm->metadataParameterMapping[$key]) && $value instanceof ClassMetadata) {
$value = $value->getMetadataValue($rsm->metadataParameterMapping[$key]);
}

if (isset($rsm->discriminatorParameters[$key]) && $value instanceof ClassMetadata) {
$value = array_keys(HierarchyDiscriminatorResolver::resolveDiscriminatorsForClass($value, $this->_em));
}

$value = $this->processParameterValue($value);
$type = ($parameter->getValue() === $value)
? $parameter->getType()
: ParameterTypeInferer::inferType($value);
[$value, $type] = $this->resolveParameterValue($parameter);

foreach ($paramMappings[$key] as $position) {
$types[$position] = $type;
Expand Down Expand Up @@ -439,6 +429,38 @@ private function processParameterMappings($paramMappings)
return [$sqlParams, $types];
}

/** @return mixed[] tuple of (value, type) */
private function resolveParameterValue(Parameter $parameter) : array
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
{
if ($parameter->typeWasSpecified()) {
return [$parameter->getValue(), $parameter->getType()];
}

$key = $parameter->getName();
$originalValue = $parameter->getValue();
$value = $originalValue;
$rsm = $this->getResultSetMapping();

assert($rsm !== null);
Ocramius marked this conversation as resolved.
Show resolved Hide resolved

if ($value instanceof ClassMetadata && isset($rsm->metadataParameterMapping[$key])) {
$value = $value->getMetadataValue($rsm->metadataParameterMapping[$key]);
}

if ($value instanceof ClassMetadata && isset($rsm->discriminatorParameters[$key])) {
$value = array_keys(HierarchyDiscriminatorResolver::resolveDiscriminatorsForClass($value, $this->_em));
}

$processedValue = $this->processParameterValue($value);

return [
$processedValue,
$originalValue === $processedValue
? $parameter->getType()
: ParameterTypeInferer::inferType($processedValue),
];
}

/**
* Defines a cache driver to be used for caching queries.
*
Expand Down
17 changes: 16 additions & 1 deletion lib/Doctrine/ORM/Query/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Doctrine\ORM\Query;

use function trim;

/**
* Defines a Query Parameter.
*
Expand Down Expand Up @@ -49,6 +51,13 @@ class Parameter
*/
private $type;

/**
* Whether the parameter type was explicitly specified or not
*
* @var bool
*/
private $typeSpecified;
Ocramius marked this conversation as resolved.
Show resolved Hide resolved

/**
* Constructor.
*
Expand All @@ -58,7 +67,8 @@ class Parameter
*/
public function __construct($name, $value, $type = null)
{
$this->name = trim($name, ':');
$this->name = trim($name, ':');
$this->typeSpecified = $type !== null;

$this->setValue($value, $type);
}
Expand Down Expand Up @@ -104,4 +114,9 @@ public function setValue($value, $type = null)
$this->value = $value;
$this->type = $type ?: ParameterTypeInferer::inferType($value);
}

public function typeWasSpecified() : bool
{
return $this->typeSpecified;
}
}
30 changes: 25 additions & 5 deletions tests/Doctrine/Tests/ORM/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@

namespace Doctrine\Tests\ORM\Query;

use DateTime;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Collections\ArrayCollection;

use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\ORM\EntityManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Internal\Hydration\IterableResult;
use Doctrine\ORM\Query\Parameter;
use Doctrine\ORM\Query\QueryException;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Mocks\DriverConnectionMock;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Mocks\StatementArrayMock;
use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\Models\Generic\DateTimeModel;
use Doctrine\Tests\OrmTestCase;

class QueryTest extends OrmTestCase
{
/** @var EntityManager */
protected $_em = null;
/** @var EntityManagerMock */
protected $_em;

protected function setUp()
{
Expand Down Expand Up @@ -400,4 +402,22 @@ public function testResultCacheProfileCanBeRemovedViaSetter() : void

self::assertAttributeSame(null, '_queryCacheProfile', $query);
}

/** @group 7527 */
public function testValuesAreNotBeingResolvedForSpecifiedParameterTypes() : void
{
$unitOfWork = $this->createMock(UnitOfWork::class);

$this->_em->setUnitOfWork($unitOfWork);

$unitOfWork
->expects(self::never())
->method('getSingleIdentifierValue');

$query = $this->_em->createQuery('SELECT d FROM ' . DateTimeModel::class . ' d WHERE d.datetime = :value');

$query->setParameter('value', new DateTime(), Type::DATETIME);

self::assertEmpty($query->getResult());
}
}
5 changes: 4 additions & 1 deletion tests/Doctrine/Tests/ORM/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,11 @@ public function testSetParameter()
->setParameter('id', 1);

$parameter = new Parameter('id', 1, ParameterTypeInferer::inferType(1));
$inferred = $qb->getParameter('id');

$this->assertEquals($parameter, $qb->getParameter('id'));
self::assertSame($parameter->getValue(), $inferred->getValue());
self::assertSame($parameter->getType(), $inferred->getType());
self::assertFalse($inferred->typeWasSpecified());
}

public function testSetParameters()
Expand Down
7 changes: 3 additions & 4 deletions tests/Doctrine/Tests/OrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Tests\Mocks;
use Doctrine\Tests\Mocks\EntityManagerMock;

/**
* Base testcase class for all ORM testcases.
Expand Down Expand Up @@ -113,10 +114,8 @@ protected function createAnnotationDriver($paths = [], $alias = null)
* @param mixed $conf
* @param \Doctrine\Common\EventManager|null $eventManager
* @param bool $withSharedMetadata
*
* @return \Doctrine\ORM\EntityManager
*/
protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true)
protected function _getTestEntityManager($conn = null, $conf = null, $eventManager = null, $withSharedMetadata = true) : EntityManagerMock
{
$metadataCache = $withSharedMetadata
? self::getSharedMetadataCacheImpl()
Expand Down Expand Up @@ -160,7 +159,7 @@ protected function _getTestEntityManager($conn = null, $conf = null, $eventManag
$conn = DriverManager::getConnection($conn, $config, $eventManager);
}

return Mocks\EntityManagerMock::create($conn, $config, $eventManager);
return EntityManagerMock::create($conn, $config, $eventManager);
}

protected function enableSecondLevelCache($log = true)
Expand Down