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

fix: ORDER BY in findFromSql #148

Merged
merged 2 commits into from May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -40,30 +40,9 @@ protected function compute()

$allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable);

$columnDescList = [];

$tableGroupName = $this->getTableGroupName($allFetchedTables);

foreach ($this->schema->getTable($this->mainTable)->getColumns() as $column) {
$columnName = $column->getName();
$columnDescList[] = [
'as' => $columnName,
'table' => $this->mainTable,
'column' => $columnName,
'type' => $column->getType(),
'tableGroup' => $tableGroupName,
];
}

$sql = 'SELECT DISTINCT '.implode(', ', array_map(function ($columnDesc) {
return $this->tdbmService->getConnection()->quoteIdentifier($this->mainTable).'.'.$this->tdbmService->getConnection()->quoteIdentifier($columnDesc['column']);
}, $columnDescList)).' FROM '.$this->from;
list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);

if (count($allFetchedTables) > 1) {
list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
} elseif ($this->orderBy) {
list(, , $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
}
$sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM '.$this->from;

// Let's compute the COUNT.
$pkColumnNames = $this->schema->getTable($this->mainTable)->getPrimaryKeyColumns();
Expand All @@ -73,20 +52,8 @@ protected function compute()

$countSql = 'SELECT COUNT(DISTINCT '.implode(', ', $pkColumnNames).') FROM '.$this->from;

if (!empty($this->filterString)) {
$sql .= ' WHERE '.$this->filterString;
$countSql .= ' WHERE '.$this->filterString;
}

if (!empty($orderString)) {
$sql .= ' ORDER BY '.$orderString;
}

if (stripos($countSql, 'GROUP BY') !== false) {
throw new TDBMException('Unsupported use of GROUP BY in SQL request.');
}

if ($columnsList !== null) {
// Add joins on inherited tables if necessary
if (count($allFetchedTables) > 1) {
$joinSql = '';
$parentFks = $this->getParentRelationshipForeignKeys($this->mainTable);
foreach ($parentFks as $fk) {
Expand All @@ -110,10 +77,20 @@ protected function compute()
);
}

$sql = 'SELECT '.implode(', ', $columnsList).' FROM ('.$sql.') AS '.$this->mainTable.' '.$joinSql;
if (!empty($orderString)) {
$sql .= ' ORDER BY '.$orderString;
}
$sql .= $joinSql;
}

if (!empty($this->filterString)) {
$sql .= ' WHERE '.$this->filterString;
$countSql .= ' WHERE '.$this->filterString;
}

if (!empty($orderString)) {
$sql .= ' ORDER BY '.$orderString;
}

if (stripos($countSql, 'GROUP BY') !== false) {
throw new TDBMException('Unsupported use of GROUP BY in SQL request.');
}

$this->magicSql = $sql;
Expand Down
21 changes: 21 additions & 0 deletions tests/Mouf/Database/TDBM/Dao/TestUserDao.php
Expand Up @@ -31,6 +31,27 @@ public function getUsersFromSqlByAlphabeticalOrder()
return $this->findFromSql('users', null, [], 'login ASC');
}

/**
* Returns the list of users by alphabetical order.
*
* @return UserBean[]
*/
public function getUsersByCountryOrder()
{
// The third parameter will be used in the "ORDER BY" clause of the SQL query.
return $this->find(null, [], 'country.label ASC', ['country']);
}
/**
* Returns the list of users by alphabetical order.
*
* @return UserBean[]
*/
public function getUsersFromSqlByCountryOrder()
{
// The third parameter will be used in the "ORDER BY" clause of the SQL query.
return $this->findFromSql('users JOIN country ON country.id = users.country_id', null, [], 'country.label ASC');
}

/**
* Returns the list of users whose login starts with $login.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Mouf/Database/TDBM/TDBMDaoGeneratorTest.php
Expand Up @@ -601,6 +601,15 @@ public function testFindOrderBy()
$this->assertCount(6, $users);
$this->assertEquals('bill.shakespeare', $users[0]->getLogin());
$this->assertEquals('jean.dupont', $users[1]->getLogin());

$users = $userDao->getUsersByCountryOrder();
$this->assertCount(6, $users);
$countryName1 = $users[0]->getCountry()->getLabel();
for ($i = 1; $i < 6; $i++) {
$countryName2 = $users[$i]->getCountry()->getLabel();
$this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
$countryName1 = $countryName2;
}
}

/**
Expand All @@ -614,6 +623,15 @@ public function testFindFromSqlOrderBy()
$this->assertCount(6, $users);
$this->assertEquals('bill.shakespeare', $users[0]->getLogin());
$this->assertEquals('jean.dupont', $users[1]->getLogin());

$users = $userDao->getUsersFromSqlByCountryOrder();
$this->assertCount(6, $users);
$countryName1 = $users[0]->getCountry()->getLabel();
for ($i = 1; $i < 6; $i++) {
$countryName2 = $users[$i]->getCountry()->getLabel();
$this->assertLessThanOrEqual(0, strcmp($countryName1, $countryName2));
$countryName1 = $countryName2;
}
}

/**
Expand Down