From f54e8470c8dd3d7bcc08e4b1730ba2957342210b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 22 Nov 2018 11:33:26 +0100 Subject: [PATCH 1/3] Adding failing test --- tests/TDBMDaoGeneratorTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/TDBMDaoGeneratorTest.php b/tests/TDBMDaoGeneratorTest.php index 4ad8bda4..f84f22c8 100644 --- a/tests/TDBMDaoGeneratorTest.php +++ b/tests/TDBMDaoGeneratorTest.php @@ -55,6 +55,7 @@ use TheCodingMachine\TDBM\Test\Dao\CountryDao; use TheCodingMachine\TDBM\Test\Dao\DogDao; use TheCodingMachine\TDBM\Test\Dao\FileDao; +use TheCodingMachine\TDBM\Test\Dao\Generated\ContactBaseDao; use TheCodingMachine\TDBM\Test\Dao\Generated\UserBaseDao; use TheCodingMachine\TDBM\Test\Dao\RefNoPrimKeyDao; use TheCodingMachine\TDBM\Test\Dao\RoleDao; @@ -1771,4 +1772,13 @@ public function testDeleteMultiPrimaryKeysBean() $this->assertCount(0, $stateDao->findAll()); } + + public function testSortOnInheritedTable() + { + $contactDao = new ContactDao($this->tdbmService); + + $contacts = $contactDao->findAll()->withOrder('users.email ASC'); + + $this->assertCount($contacts, 5); + } } From 94b0ddf206e8793f401d1e9c16f7c8df7938d637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 22 Nov 2018 14:59:07 +0100 Subject: [PATCH 2/3] Adding failing test --- src/QueryFactory/AbstractQueryFactory.php | 4 +++- tests/TDBMAbstractServiceTest.php | 4 ++++ tests/TDBMDaoGeneratorTest.php | 15 ++++++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/QueryFactory/AbstractQueryFactory.php b/src/QueryFactory/AbstractQueryFactory.php index 2ae6e421..d4a6bd96 100644 --- a/src/QueryFactory/AbstractQueryFactory.php +++ b/src/QueryFactory/AbstractQueryFactory.php @@ -3,6 +3,7 @@ namespace TheCodingMachine\TDBM\QueryFactory; +use function array_unique; use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Schema\Schema; use TheCodingMachine\TDBM\OrderByAnalyzer; @@ -105,6 +106,7 @@ protected function getColumnsList(string $mainTable, array $additionalTablesFetc if ($orderByColumn['type'] === 'colref') { if ($orderByColumn['table'] !== null) { if ($canAddAdditionalTablesFetch) { + // FIXME: do not ADD if already in inherited tables! $additionalTablesFetch[] = $orderByColumn['table']; } else { $sortColumnName = 'sort_column_'.$sortColumn; @@ -150,7 +152,7 @@ protected function getColumnsList(string $mainTable, array $additionalTablesFetc } // Let's remove any duplicate - $allFetchedTables = array_flip(array_flip($allFetchedTables)); + $allFetchedTables = array_unique($allFetchedTables); // We quote in MySQL because MagicJoin requires MySQL style quotes $mysqlPlatform = new MySqlPlatform(); diff --git a/tests/TDBMAbstractServiceTest.php b/tests/TDBMAbstractServiceTest.php index 676a4708..2dde8e9b 100644 --- a/tests/TDBMAbstractServiceTest.php +++ b/tests/TDBMAbstractServiceTest.php @@ -295,6 +295,10 @@ private static function initSchema(Connection $connection): void ->extends('animal') ->column('cuteness_level')->integer()->null(); + $db->table('panda') + ->extends('animal') + ->column('weight')->float()->null(); + $db->table('boats') ->column('id')->integer()->primaryKey()->autoIncrement()->comment('@Autoincrement') ->column('name')->string(255) diff --git a/tests/TDBMDaoGeneratorTest.php b/tests/TDBMDaoGeneratorTest.php index 29b2bd52..7b75af45 100644 --- a/tests/TDBMDaoGeneratorTest.php +++ b/tests/TDBMDaoGeneratorTest.php @@ -33,6 +33,7 @@ use TheCodingMachine\TDBM\Test\Dao\AllNullableDao; use TheCodingMachine\TDBM\Test\Dao\AnimalDao; use TheCodingMachine\TDBM\Test\Dao\Bean\AllNullableBean; +use TheCodingMachine\TDBM\Test\Dao\Bean\AnimalBean; use TheCodingMachine\TDBM\Test\Dao\Bean\Article2Bean; use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean; use TheCodingMachine\TDBM\Test\Dao\Bean\BoatBean; @@ -1781,12 +1782,20 @@ public function testDeleteMultiPrimaryKeysBean() } + /** + * @depends testDaoGeneration + */ public function testSortOnInheritedTable() { - $contactDao = new ContactDao($this->tdbmService); + $animalDao = new AnimalDao($this->tdbmService); + + // Let's insert an animal that is nothing. + $animal = new AnimalBean('Mickey'); + $animalDao->save($animal); - $contacts = $contactDao->findAll()->withOrder('users.email ASC'); + $animals = $animalDao->findAll()->withOrder('dog.race ASC'); - $this->assertCount($contacts, 5); + $animalsArr = $animals->toArray(); + $this->assertCount(2, $animalsArr); } } From 78982b4a8bdfc9ca50b21dddaad1053c236ae058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Thu, 22 Nov 2018 15:07:42 +0100 Subject: [PATCH 3/3] Fixing sorting on child column --- src/QueryFactory/AbstractQueryFactory.php | 6 +++++- tests/TDBMDaoGeneratorTest.php | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/QueryFactory/AbstractQueryFactory.php b/src/QueryFactory/AbstractQueryFactory.php index d4a6bd96..e8f4aa2c 100644 --- a/src/QueryFactory/AbstractQueryFactory.php +++ b/src/QueryFactory/AbstractQueryFactory.php @@ -6,6 +6,7 @@ use function array_unique; use Doctrine\DBAL\Platforms\MySqlPlatform; use Doctrine\DBAL\Schema\Schema; +use function in_array; use TheCodingMachine\TDBM\OrderByAnalyzer; use TheCodingMachine\TDBM\TDBMInvalidArgumentException; use TheCodingMachine\TDBM\TDBMService; @@ -106,7 +107,6 @@ protected function getColumnsList(string $mainTable, array $additionalTablesFetc if ($orderByColumn['type'] === 'colref') { if ($orderByColumn['table'] !== null) { if ($canAddAdditionalTablesFetch) { - // FIXME: do not ADD if already in inherited tables! $additionalTablesFetch[] = $orderByColumn['table']; } else { $sortColumnName = 'sort_column_'.$sortColumn; @@ -143,6 +143,10 @@ protected function getColumnsList(string $mainTable, array $additionalTablesFetc } foreach ($additionalTablesFetch as $additionalTable) { + if (in_array($additionalTable, $allFetchedTables, true)) { + continue; + } + $relatedTables = $this->tdbmService->_getRelatedTablesByInheritance($additionalTable); $tableGroupName = $this->getTableGroupName($relatedTables); foreach ($relatedTables as $table) { diff --git a/tests/TDBMDaoGeneratorTest.php b/tests/TDBMDaoGeneratorTest.php index 7b75af45..7657e109 100644 --- a/tests/TDBMDaoGeneratorTest.php +++ b/tests/TDBMDaoGeneratorTest.php @@ -1796,6 +1796,6 @@ public function testSortOnInheritedTable() $animals = $animalDao->findAll()->withOrder('dog.race ASC'); $animalsArr = $animals->toArray(); - $this->assertCount(2, $animalsArr); + $this->assertCount(3, $animalsArr); } }