Skip to content

Commit 3178a48

Browse files
authored
Merge pull request #50 from moufmouf/findfromsql_orderby_inherited
findFromSql with an orderBy on an inherited table does now work.
2 parents 3fc24cf + 1936a36 commit 3178a48

7 files changed

+63
-10
lines changed

src/QueryFactory/AbstractQueryFactory.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAna
5151
*
5252
* Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause.
5353
*
54-
* @param string $mainTable
55-
* @param array $additionalTablesFetch
54+
* @param string $mainTable
55+
* @param array $additionalTablesFetch
5656
* @param string|UncheckedOrderBy|null $orderBy
5757
*
58+
* @param bool $canAddAdditionalTablesFetch Set to true if the function can add additional tables to fetch (so if the factory generates its own FROM clause)
5859
* @return array
59-
*
60-
* @throws \Doctrine\DBAL\Schema\SchemaException
6160
*/
62-
protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null)
61+
protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null, bool $canAddAdditionalTablesFetch = false)
6362
{
6463
// From the table name and the additional tables we want to fetch, let's build a list of all tables
6564
// that must be part of the select columns.
@@ -102,7 +101,17 @@ protected function getColumnsList(string $mainTable, array $additionalTablesFetc
102101
foreach ($orderByColumns as $orderByColumn) {
103102
if ($orderByColumn['type'] === 'colref') {
104103
if ($orderByColumn['table'] !== null) {
105-
$additionalTablesFetch[] = $orderByColumn['table'];
104+
if ($canAddAdditionalTablesFetch) {
105+
$additionalTablesFetch[] = $orderByColumn['table'];
106+
} else {
107+
$sortColumnName = 'sort_column_'.$sortColumn;
108+
$mysqlPlatform = new MySqlPlatform();
109+
$columnsList[] = $mysqlPlatform->quoteIdentifier($orderByColumn['table']).'.'.$mysqlPlatform->quoteIdentifier($orderByColumn['column']).' as '.$sortColumnName;
110+
$columnDescList[$sortColumnName] = [
111+
'tableGroup' => null,
112+
];
113+
++$sortColumn;
114+
}
106115
}
107116
if ($securedOrderBy) {
108117
// Let's protect via MySQL since we go through MagicJoin

src/QueryFactory/FindObjectsFromSqlQueryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function compute()
4242

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

45-
list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy);
45+
list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy, false);
4646

4747
$sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM '.$this->from;
4848

src/QueryFactory/FindObjectsQueryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(string $mainTable, array $additionalTablesFetch, $fi
2525

2626
protected function compute()
2727
{
28-
list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy);
28+
list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy, true);
2929

3030
$sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')';
3131

tests/Dao/TestArticleDao.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace TheCodingMachine\TDBM\Dao;
4+
5+
use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean;
6+
use TheCodingMachine\TDBM\Test\Dao\Generated\ArticleBaseDao;
7+
8+
/**
9+
* The UserDao class will maintain the persistence of UserBean class into the users table.
10+
*/
11+
class TestArticleDao extends ArticleBaseDao
12+
{
13+
/**
14+
* Used to test a findFromSql with an order by clause on an inherited table.
15+
*
16+
* @return ArticleBean[]
17+
*/
18+
public function getArticlesByUserLogin()
19+
{
20+
return $this->findFromSql(
21+
'article JOIN users ON article.author_id = users.id',
22+
null,
23+
[],
24+
'users.login'
25+
);
26+
}
27+
}

tests/TDBMAbstractServiceTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ private static function initSchema(Connection $connection): void
310310

311311
$db->table('article')
312312
->column('id')->string(36)->primaryKey()->comment('@UUID')
313-
->column('content')->string(255);
313+
->column('content')->string(255)
314+
->column('author_id')->references('users')->null();
314315

315316
$db->table('article2')
316317
->column('id')->string(36)->primaryKey()->comment('@UUID v4')

tests/TDBMDaoGeneratorTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
2626
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
2727
use Ramsey\Uuid\Uuid;
28+
use TheCodingMachine\TDBM\Dao\TestArticleDao;
2829
use TheCodingMachine\TDBM\Dao\TestCountryDao;
2930
use TheCodingMachine\TDBM\Dao\TestRoleDao;
3031
use TheCodingMachine\TDBM\Dao\TestUserDao;
@@ -641,6 +642,21 @@ public function testFindFromSqlOrderBy()
641642
}
642643
}
643644

645+
/**
646+
* @depends testDaoGeneration
647+
*/
648+
public function testFindFromSqlOrderByOnInheritedBean()
649+
{
650+
$articleDao = new TestArticleDao($this->tdbmService);
651+
$articles = $articleDao->getArticlesByUserLogin();
652+
653+
foreach ($articles as $article) {
654+
var_dump($article);
655+
}
656+
$this->assertCount(0, $articles);
657+
}
658+
659+
644660
/**
645661
* @depends testDaoGeneration
646662
*/

tests/TDBMSchemaAnalyzerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testGetIncomingForeignKeys()
5858
$tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer);
5959

6060
$fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('users');
61-
$this->assertCount(0, $fks);
61+
$this->assertCount(1, $fks);
6262
}
6363

6464
public function testGetIncomingForeignKeys2()

0 commit comments

Comments
 (0)