diff --git a/CHANGELOG.md b/CHANGELOG.md index d33dbe9..b0f5a2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## Unreleased + +### Added + +- Added parentQuery field to Select Object +- New test for orderBy method in Select Class + +### Altered + +- Changed orderBy method logic in Select Class. Now every select query has access to his parent object. You can manipulate sequence of your orderBy clause. + ## 1.0.2 - TBA ### Added @@ -27,4 +38,4 @@ ## 0.0.5-alpha - 2014-07-01 -- Initial release \ No newline at end of file +- Initial release diff --git a/src/Manipulation/JoinQuery.php b/src/Manipulation/JoinQuery.php index 94535e8..e478027 100644 --- a/src/Manipulation/JoinQuery.php +++ b/src/Manipulation/JoinQuery.php @@ -101,6 +101,7 @@ public function join( $select = QueryFactory::createSelect($table); $select->setColumns($columns); $select->setJoinType($joinType); + $select->setParentQuery($this->select); $this->addJoin($select, $selfColumn, $refColumn); } diff --git a/src/Manipulation/Select.php b/src/Manipulation/Select.php index fa5dd53..1580df4 100644 --- a/src/Manipulation/Select.php +++ b/src/Manipulation/Select.php @@ -13,6 +13,7 @@ use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory; use NilPortugues\Sql\QueryBuilder\Syntax\Table; use NilPortugues\Sql\QueryBuilder\Syntax\Where; +use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy; /** * Class Select. @@ -64,6 +65,11 @@ class Select extends AbstractBaseQuery */ protected $columnQuery; + /** + * @var ParentQuery + */ + protected $parentQuery; + /** * @param string $table * @param array $columns @@ -499,12 +505,42 @@ public function isDistinct() */ public function getAllOrderBy() { - $order = $this->orderBy; + return $this->orderBy; + } - foreach ($this->joinQuery->getJoins() as $join) { - $order = \array_merge($order, $join->getAllOrderBy()); - } + /** + * @return ParentQuery + */ + public function getParentQuery() + { + return $this->parentQuery; + } - return $order; + /** + * @param Select $parentQuery + * + * @return $this + */ + public function setParentQuery(Select $parentQuery) + { + $this->parentQuery = $parentQuery; + + return $this; + } + + /** + * @param string $column + * @param string $direction + * @param null $table + * + * @return $this + */ + public function orderBy($column, $direction = OrderBy::ASC, $table = null) + { + $current = parent::orderBy($column, $direction, $table); + if ($this->getParentQuery() != null) { + $this->getParentQuery()->orderBy($column, $direction, \is_null($table) ? $this->getTable() : $table); + } + return $current; } } diff --git a/tests/Manipulation/SelectTest.php b/tests/Manipulation/SelectTest.php index 82c491f..fe445b9 100644 --- a/tests/Manipulation/SelectTest.php +++ b/tests/Manipulation/SelectTest.php @@ -11,6 +11,7 @@ namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation; use NilPortugues\Sql\QueryBuilder\Manipulation\Select; +use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy; /** * Class SelectTest. @@ -35,4 +36,66 @@ public function itShouldGetPartName() { $this->assertSame('SELECT', $this->query->partName()); } + + /** + * @test + */ + public function itShouldSetParentOrderByAlso() + { + $columns = [ + 'id', + 'phase_id', + 'league_id', + 'date', + ]; + $parentTable = 'events'; + $this->query->setTable($parentTable); + $this->query->setColumns($columns); + + $sorts = [ + [ + 'field' => 'league_id', + 'direction' => 1, + ], + [ + 'field' => 'start_date', + 'direction' => 0, + 'table' => 'phases', + 'joinBy' => 'phase_id', + 'joinWith' => 'id', + ], + [ + 'field' => 'date', + 'direction' => 1, + ], + ]; + + if (is_array($sorts)) { + foreach ($sorts as $sort) { + $order = (int)$sort['direction'] > 0 ? OrderBy::ASC : OrderBy::DESC; + if (count($sort) == 5) { + $this->query->leftJoin( + $sort['table'], + $sort['joinBy'], + $sort['joinWith'] + )->orderBy($sort['field'], $order); + } else { + $this->query->orderBy($sort['field'], $order); + } + } + } + + $returnedOrders = $this->query->getAllOrderBy(); + foreach ($returnedOrders as $id => $orderByObject) { + $column = $orderByObject->getColumn(); + $table = $column->getTable(); + $expectedColumn = $sorts[$id]['field']; + $expectedTable = array_key_exists('table', $sorts[$id]) ? $sorts[$id]['table'] : $parentTable; + $expectedDirection = (int)$sorts[$id]['direction'] > 0 ? OrderBy::ASC : OrderBy::DESC; + $this->assertSame($expectedColumn, $column->getName()); + $this->assertSame($expectedTable, $table->getName()); + $this->assertSame($expectedDirection, $orderByObject->getDirection()); + } + $this->assertCount(count($sorts), $returnedOrders); + } }