Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -27,4 +38,4 @@

## 0.0.5-alpha - 2014-07-01

- Initial release
- Initial release
1 change: 1 addition & 0 deletions src/Manipulation/JoinQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
46 changes: 41 additions & 5 deletions src/Manipulation/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -64,6 +65,11 @@ class Select extends AbstractBaseQuery
*/
protected $columnQuery;

/**
* @var ParentQuery
*/
protected $parentQuery;

/**
* @param string $table
* @param array $columns
Expand Down Expand Up @@ -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;
}
}
63 changes: 63 additions & 0 deletions tests/Manipulation/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;

use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;

/**
* Class SelectTest.
Expand All @@ -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);
}
}