diff --git a/lib/Doctrine/ORM/QueryBuilder.php b/lib/Doctrine/ORM/QueryBuilder.php index e547814ff..bcea7c000 100644 --- a/lib/Doctrine/ORM/QueryBuilder.php +++ b/lib/Doctrine/ORM/QueryBuilder.php @@ -79,6 +79,11 @@ class QueryBuilder * @var array The query parameters. */ private $_params = array(); + + /** + * @var array The parameter type map of this query. + */ + private $_paramTypes = array(); /** * @var integer The index of the first result to retrieve. @@ -208,7 +213,7 @@ class QueryBuilder public function getQuery() { return $this->_em->createQuery($this->getDQL()) - ->setParameters($this->_params) + ->setParameters($this->_params, $this->_paramTypes) ->setFirstResult($this->_firstResult) ->setMaxResults($this->_maxResults); } @@ -246,10 +251,14 @@ class QueryBuilder * * @param string|integer $key The parameter position or name. * @param mixed $value The parameter value. + * @param string|null $type PDO::PARAM_* or \Doctrine\DBAL\Types\Type::* constant * @return QueryBuilder This QueryBuilder instance. */ - public function setParameter($key, $value) + public function setParameter($key, $value, $type = null) { + if ($type !== null) { + $this->_paramTypes[$key] = $type; + } $this->_params[$key] = $value; return $this; } @@ -271,8 +280,9 @@ class QueryBuilder * @param array $params The query parameters to set. * @return QueryBuilder This QueryBuilder instance. */ - public function setParameters(array $params) + public function setParameters(array $params, array $types = array()) { + $this->_paramTypes = $types; $this->_params = $params; return $this; } diff --git a/tests/Doctrine/Tests/ORM/Functional/TypeTest.php b/tests/Doctrine/Tests/ORM/Functional/TypeTest.php index a4ef84589..d20922894 100644 --- a/tests/Doctrine/Tests/ORM/Functional/TypeTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/TypeTest.php @@ -8,6 +8,7 @@ use Doctrine\Tests\Models\Generic\DecimalModel; use Doctrine\Tests\Models\Generic\SerializationModel; use Doctrine\ORM\Mapping\AssociationMapping; +use Doctrine\DBAL\Types\Type; require_once __DIR__ . '/../../TestInit.php'; @@ -122,6 +123,41 @@ class TypeTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals('2009-10-02 20:10:52', $dateTimeDb->datetime->format('Y-m-d H:i:s')); } + public function testDqlQueryBindDateTimeInstance() + { + $date = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin')); + + $dateTime = new DateTimeModel(); + $dateTime->datetime = $date; + + $this->_em->persist($dateTime); + $this->_em->flush(); + $this->_em->clear(); + + $dateTimeDb = $this->_em->createQuery('SELECT d FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime = ?1') + ->setParameter(1, $date, Type::DATETIME) + ->getSingleResult(); + } + + public function testDqlQueryBuilderBindDateTimeInstance() + { + $date = new \DateTime('2009-10-02 20:10:52', new \DateTimeZone('Europe/Berlin')); + + $dateTime = new DateTimeModel(); + $dateTime->datetime = $date; + + $this->_em->persist($dateTime); + $this->_em->flush(); + $this->_em->clear(); + + $dateTimeDb = $this->_em->createQueryBuilder() + ->select('d') + ->from('Doctrine\Tests\Models\Generic\DateTimeModel', 'd') + ->where('d.datetime = ?1') + ->setParameter(1, $date, Type::DATETIME) + ->getQuery()->getSingleResult(); + } + public function testTime() { $dateTime = new DateTimeModel();