From a2cc9f0f6d780325bd960e829129e186199330bd Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 2 Jan 2011 09:38:32 +0100 Subject: [PATCH] DDC-949 - Bugfix for BasicEntityPersister not using $types for select clauses. This fixes the issue for PostgreSQL however it still occurs on Oracle. DBAL change is necessary for this. --- .../ORM/Persisters/BasicEntityPersister.php | 41 ++++++++++++++---- .../ORM/Functional/Ticket/DDC949Test.php | 43 +++++++++++++++++++ 2 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC949Test.php diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 2903ac96f..c43973189 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -525,7 +525,8 @@ class BasicEntityPersister public function load(array $criteria, $entity = null, $assoc = null, array $hints = array(), $lockMode = 0) { $sql = $this->_getSelectEntitiesSQL($criteria, $assoc, $lockMode); - $stmt = $this->_conn->executeQuery($sql, array_values($criteria)); + list($params, $types) = $this->expandParameters($criteria); + $stmt = $this->_conn->executeQuery($sql, $params, $types); $result = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -608,7 +609,8 @@ class BasicEntityPersister public function refresh(array $id, $entity) { $sql = $this->_getSelectEntitiesSQL($id); - $stmt = $this->_conn->executeQuery($sql, array_values($id)); + list($params, $types) = $this->expandParameters($id); + $stmt = $this->_conn->executeQuery($sql, $params, $types); $result = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -694,7 +696,8 @@ class BasicEntityPersister { $entities = array(); $sql = $this->_getSelectEntitiesSQL($criteria); - $stmt = $this->_conn->executeQuery($sql, array_values($criteria)); + list($params, $types) = $this->expandParameters($criteria); + $stmt = $this->_conn->executeQuery($sql, $params, $types); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); $stmt->closeCursor(); @@ -742,7 +745,8 @@ class BasicEntityPersister } $sql = $this->_getSelectEntitiesSQL($criteria, $assoc); - $stmt = $this->_conn->executeQuery($sql, array_values($criteria)); + list($params, $types) = $this->expandParameters($criteria); + $stmt = $this->_conn->executeQuery($sql, $params, $types); while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) { $coll->hydrateAdd($this->_createEntity($result)); } @@ -1084,8 +1088,8 @@ class BasicEntityPersister $sql = 'SELECT 1 ' . $this->_platform->appendLockHint($this->getLockTablesSql(), $lockMode) . ($conditionSql ? ' WHERE ' . $conditionSql : '') . ' ' . $lockSql; - $params = array_values($criteria); - $this->_conn->executeQuery($sql, $params); + list($params, $types) = $this->expandParameters($criteria); + $stmt = $this->_conn->executeQuery($sql, $params, $types); } /** @@ -1169,14 +1173,35 @@ class BasicEntityPersister } $sql = $this->_getSelectEntitiesSQL($criteria, $assoc); - $params = array_values($criteria); - $stmt = $this->_conn->executeQuery($sql, $params); + list($params, $types) = $this->expandParameters($criteria); + $stmt = $this->_conn->executeQuery($sql, $params, $types); while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) { $coll->hydrateAdd($this->_createEntity($result)); } $stmt->closeCursor(); } + /** + * Expand the parameters from the given criteria and use the correct binding types if found. + * + * @param array $criteria + * @return array + */ + private function expandParameters($criteria) + { + $params = $types = array(); + + foreach ($criteria AS $field => $value) { + $type = null; + if (isset($this->_class->fieldMappings[$field])) { + $type = Type::getType($this->_class->fieldMappings[$field]['type'])->getBindingType(); + } + $params[] = $value; + $types[] = $type; + } + return array($params, $types); + } + /** * Checks whether the given managed entity exists in the database. * diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC949Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC949Test.php new file mode 100644 index 000000000..adab29d93 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC949Test.php @@ -0,0 +1,43 @@ +useModelSet('generic'); + parent::setUp(); + } + + /** + * @group DDC-949 + */ + public function testBooleanThroughRepository() + { + $true = new BooleanModel(); + $true->booleanField = true; + + $false = new BooleanModel(); + $false->booleanField = false; + + $this->_em->persist($true); + $this->_em->persist($false); + $this->_em->flush(); + $this->_em->clear(); + + $true = $this->_em->getRepository('Doctrine\Tests\Models\Generic\BooleanModel')->findOneBy(array('booleanField' => true)); + $false = $this->_em->getRepository('Doctrine\Tests\Models\Generic\BooleanModel')->findOneBy(array('booleanField' => false)); + + $this->assertType('Doctrine\Tests\Models\Generic\BooleanModel', $true); + $this->assertTrue($true->booleanField, "True Boolean Model should be true."); + + $this->assertType('Doctrine\Tests\Models\Generic\BooleanModel', $false); + $this->assertFalse($false->booleanField, "False Boolean Model should be false."); + } +} \ No newline at end of file