From b75ef330d4191efd46b134a2eae1ba772e008384 Mon Sep 17 00:00:00 2001 From: zYne Date: Wed, 1 Nov 2006 17:08:35 +0000 Subject: [PATCH] Fixed pgsql limit subquery algorithm --- lib/Doctrine/Query.php | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/Doctrine/Query.php b/lib/Doctrine/Query.php index 1a4e683b6..f388f790b 100644 --- a/lib/Doctrine/Query.php +++ b/lib/Doctrine/Query.php @@ -530,17 +530,23 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { * @return string the limit subquery */ public function getLimitSubquery() { - $k = array_keys($this->tables); - $table = $this->tables[$k[0]]; + $k = array_keys($this->tables); + $table = $this->tables[$k[0]]; + + $primaryKey = $table->getTableName() . '.' . $table->getIdentifier(); + + // initialize the base of the subquery + $subquery = 'SELECT DISTINCT ' . $primaryKey; - $subquery = 'SELECT DISTINCT ' . $table->getTableName() - . '.' . $table->getIdentifier(); - if($this->connection->getDBH()->getAttribute(PDO::ATTR_DRIVER_NAME) == 'pgsql') { + // pgsql needs the order by fields to be preserved in select clause + foreach($this->parts['orderby'] as $part) { $e = explode(' ', $part); - - $subquery .= ', ' . $e[0]; + + // don't add primarykey column (its already in the select clause) + if($e[0] !== $primaryKey) + $subquery .= ', ' . $e[0]; } }