diff --git a/src/QueryFactory/AbstractQueryFactory.php b/src/QueryFactory/AbstractQueryFactory.php index fc9057a1..587eb08c 100644 --- a/src/QueryFactory/AbstractQueryFactory.php +++ b/src/QueryFactory/AbstractQueryFactory.php @@ -51,15 +51,14 @@ public function __construct(TDBMService $tdbmService, Schema $schema, OrderByAna * * Note: MySQL dictates that ORDER BYed columns should appear in the SELECT clause. * - * @param string $mainTable - * @param array $additionalTablesFetch + * @param string $mainTable + * @param array $additionalTablesFetch * @param string|UncheckedOrderBy|null $orderBy * + * @param bool $canAddAdditionalTablesFetch Set to true if the function can add additional tables to fetch (so if the factory generates its own FROM clause) * @return array - * - * @throws \Doctrine\DBAL\Schema\SchemaException */ - protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null) + protected function getColumnsList(string $mainTable, array $additionalTablesFetch = array(), $orderBy = null, bool $canAddAdditionalTablesFetch = false) { // From the table name and the additional tables we want to fetch, let's build a list of all tables // that must be part of the select columns. @@ -102,7 +101,17 @@ protected function getColumnsList(string $mainTable, array $additionalTablesFetc foreach ($orderByColumns as $orderByColumn) { if ($orderByColumn['type'] === 'colref') { if ($orderByColumn['table'] !== null) { - $additionalTablesFetch[] = $orderByColumn['table']; + if ($canAddAdditionalTablesFetch) { + $additionalTablesFetch[] = $orderByColumn['table']; + } else { + $sortColumnName = 'sort_column_'.$sortColumn; + $mysqlPlatform = new MySqlPlatform(); + $columnsList[] = $mysqlPlatform->quoteIdentifier($orderByColumn['table']).'.'.$mysqlPlatform->quoteIdentifier($orderByColumn['column']).' as '.$sortColumnName; + $columnDescList[$sortColumnName] = [ + 'tableGroup' => null, + ]; + ++$sortColumn; + } } if ($securedOrderBy) { // Let's protect via MySQL since we go through MagicJoin diff --git a/src/QueryFactory/FindObjectsFromSqlQueryFactory.php b/src/QueryFactory/FindObjectsFromSqlQueryFactory.php index 0575a729..a65b1192 100644 --- a/src/QueryFactory/FindObjectsFromSqlQueryFactory.php +++ b/src/QueryFactory/FindObjectsFromSqlQueryFactory.php @@ -42,7 +42,7 @@ protected function compute() $allFetchedTables = $this->tdbmService->_getRelatedTablesByInheritance($this->mainTable); - list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy); + list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, [], $this->orderBy, false); $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM '.$this->from; diff --git a/src/QueryFactory/FindObjectsQueryFactory.php b/src/QueryFactory/FindObjectsQueryFactory.php index a1b8fb7c..3d0cbd89 100644 --- a/src/QueryFactory/FindObjectsQueryFactory.php +++ b/src/QueryFactory/FindObjectsQueryFactory.php @@ -25,7 +25,7 @@ public function __construct(string $mainTable, array $additionalTablesFetch, $fi protected function compute() { - list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy); + list($columnDescList, $columnsList, $orderString) = $this->getColumnsList($this->mainTable, $this->additionalTablesFetch, $this->orderBy, true); $sql = 'SELECT DISTINCT '.implode(', ', $columnsList).' FROM MAGICJOIN('.$this->mainTable.')'; diff --git a/tests/Dao/TestArticleDao.php b/tests/Dao/TestArticleDao.php new file mode 100644 index 00000000..84022d71 --- /dev/null +++ b/tests/Dao/TestArticleDao.php @@ -0,0 +1,27 @@ +findFromSql( + 'article JOIN users ON article.author_id = users.id', + null, + [], + 'users.login' + ); + } +} diff --git a/tests/TDBMAbstractServiceTest.php b/tests/TDBMAbstractServiceTest.php index 20aed35f..2ec6287f 100644 --- a/tests/TDBMAbstractServiceTest.php +++ b/tests/TDBMAbstractServiceTest.php @@ -310,7 +310,8 @@ private static function initSchema(Connection $connection): void $db->table('article') ->column('id')->string(36)->primaryKey()->comment('@UUID') - ->column('content')->string(255); + ->column('content')->string(255) + ->column('author_id')->references('users')->null(); $db->table('article2') ->column('id')->string(36)->primaryKey()->comment('@UUID v4') diff --git a/tests/TDBMDaoGeneratorTest.php b/tests/TDBMDaoGeneratorTest.php index dba3ab43..2c2fac88 100644 --- a/tests/TDBMDaoGeneratorTest.php +++ b/tests/TDBMDaoGeneratorTest.php @@ -25,6 +25,7 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer; use Ramsey\Uuid\Uuid; +use TheCodingMachine\TDBM\Dao\TestArticleDao; use TheCodingMachine\TDBM\Dao\TestCountryDao; use TheCodingMachine\TDBM\Dao\TestRoleDao; use TheCodingMachine\TDBM\Dao\TestUserDao; @@ -641,6 +642,21 @@ public function testFindFromSqlOrderBy() } } + /** + * @depends testDaoGeneration + */ + public function testFindFromSqlOrderByOnInheritedBean() + { + $articleDao = new TestArticleDao($this->tdbmService); + $articles = $articleDao->getArticlesByUserLogin(); + + foreach ($articles as $article) { + var_dump($article); + } + $this->assertCount(0, $articles); + } + + /** * @depends testDaoGeneration */ diff --git a/tests/TDBMSchemaAnalyzerTest.php b/tests/TDBMSchemaAnalyzerTest.php index c34c40c5..c3781a79 100644 --- a/tests/TDBMSchemaAnalyzerTest.php +++ b/tests/TDBMSchemaAnalyzerTest.php @@ -58,7 +58,7 @@ public function testGetIncomingForeignKeys() $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer(self::getConnection(), $cache, $schemaAnalyzer); $fks = $tdbmSchemaAnalyzer->getIncomingForeignKeys('users'); - $this->assertCount(0, $fks); + $this->assertCount(1, $fks); } public function testGetIncomingForeignKeys2()