Skip to content

Commit

Permalink
Merge pull request #112 from moufmouf/sort_on_children
Browse files Browse the repository at this point in the history
Fixing sort on child error
  • Loading branch information
moufmouf authored Nov 22, 2018
2 parents 45ed32e + 78982b4 commit 1a4666a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/QueryFactory/AbstractQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace TheCodingMachine\TDBM\QueryFactory;

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;
Expand Down Expand Up @@ -141,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) {
Expand All @@ -150,7 +156,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();
Expand Down
4 changes: 4 additions & 0 deletions tests/TDBMAbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,6 +56,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;
Expand Down Expand Up @@ -1779,4 +1781,21 @@ public function testDeleteMultiPrimaryKeysBean()
$this->assertCount(0, $stateDao->findAll());

}

/**
* @depends testDaoGeneration
*/
public function testSortOnInheritedTable()
{
$animalDao = new AnimalDao($this->tdbmService);

// Let's insert an animal that is nothing.
$animal = new AnimalBean('Mickey');
$animalDao->save($animal);

$animals = $animalDao->findAll()->withOrder('dog.race ASC');

$animalsArr = $animals->toArray();
$this->assertCount(3, $animalsArr);
}
}

0 comments on commit 1a4666a

Please sign in to comment.