Skip to content

Commit

Permalink
Merge pull request #50 from moufmouf/findfromsql_orderby_inherited
Browse files Browse the repository at this point in the history
findFromSql with an orderBy on an inherited table does now work.
  • Loading branch information
moufmouf authored Oct 18, 2017
2 parents 3fc24cf + 1936a36 commit 3178a48
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 10 deletions.
21 changes: 15 additions & 6 deletions src/QueryFactory/AbstractQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/QueryFactory/FindObjectsFromSqlQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/QueryFactory/FindObjectsQueryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.')';

Expand Down
27 changes: 27 additions & 0 deletions tests/Dao/TestArticleDao.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace TheCodingMachine\TDBM\Dao;

use TheCodingMachine\TDBM\Test\Dao\Bean\ArticleBean;
use TheCodingMachine\TDBM\Test\Dao\Generated\ArticleBaseDao;

/**
* The UserDao class will maintain the persistence of UserBean class into the users table.
*/
class TestArticleDao extends ArticleBaseDao
{
/**
* Used to test a findFromSql with an order by clause on an inherited table.
*
* @return ArticleBean[]
*/
public function getArticlesByUserLogin()
{
return $this->findFromSql(
'article JOIN users ON article.author_id = users.id',
null,
[],
'users.login'
);
}
}
3 changes: 2 additions & 1 deletion tests/TDBMAbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
16 changes: 16 additions & 0 deletions tests/TDBMDaoGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/TDBMSchemaAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 3178a48

Please sign in to comment.