diff --git a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php index ca8d7178f..794ab5a53 100644 --- a/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php +++ b/lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php @@ -412,14 +412,33 @@ class LimitSubqueryOutputWalker extends SqlWalker // Generate search patterns for each field's path expression in the order by clause foreach($this->_rsm->fieldMappings as $fieldAlias => $columnName) { $dqlAliasForFieldAlias = $this->_rsm->columnOwnerMap[$fieldAlias]; + $class = $dqlAliasToClassMap[$dqlAliasForFieldAlias]; + + // If the field is from a joined child table, we won't be ordering + // on it. + if (!isset($class->fieldMappings[$columnName])) { + continue; + } + + // Get the proper column name as will appear in the select list $columnName = $this->_quoteStrategy->getColumnName( $columnName, $dqlAliasToClassMap[$dqlAliasForFieldAlias], $this->_em->getConnection()->getDatabasePlatform() ); + // Get the SQL table alias for the entity and field $sqlTableAliasForFieldAlias = $dqlAliasToSqlTableAliasMap[$dqlAliasForFieldAlias]; + $fieldMapping = $class->fieldMappings[$columnName]; + if (isset($fieldMapping['declared']) && $fieldMapping['declared'] !== $class->name) { + // Field was declared in a parent class, so we need to get the proper SQL table alias + // for the joined parent table. + $otherClassMetadata = $this->_em->getClassMetadata($fieldMapping['declared']); + $sqlTableAliasForFieldAlias = $this->getSQLTableAlias($otherClassMetadata->getTableName(), $dqlAliasForFieldAlias); + } + + // Compose search/replace patterns $searchPatterns[] = sprintf($fieldSearchPattern, $sqlTableAliasForFieldAlias, $columnName); $replacements[] = $fieldAlias; } diff --git a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php index 57ff827c8..b7db9b1f6 100644 --- a/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/PaginationTest.php @@ -8,6 +8,7 @@ use Doctrine\Tests\Models\CMS\CmsEmail; use Doctrine\Tests\Models\CMS\CmsUser; use Doctrine\Tests\Models\CMS\CmsGroup; use Doctrine\ORM\Tools\Pagination\Paginator; +use Doctrine\Tests\Models\Company\CompanyManager; use Doctrine\Tests\Models\Pagination\Company; use Doctrine\Tests\Models\Pagination\Department; use Doctrine\Tests\Models\Pagination\Logo; @@ -725,6 +726,14 @@ class PaginationTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->persist($user); } + $manager = new CompanyManager(); + $manager->setName('Roman B.'); + $manager->setTitle('Foo'); + $manager->setDepartment('IT'); + $manager->setSalary(100000); + + $this->_em->persist($manager); + $this->_em->flush(); }