Skip to content

Commit 8df6531

Browse files
author
Stanisław Śledziona
committed
select orderby logic has changed
1 parent 23afc1e commit 8df6531

File tree

4 files changed

+117
-6
lines changed

4 files changed

+117
-6
lines changed

CHANGELOG.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Added
6+
7+
- Added parentQuery field to Select Object
8+
- New test for orderBy method in Select Class
9+
10+
### Altered
11+
12+
- 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.
13+
314
## 1.0.2 - TBA
415

516
### Added
@@ -27,4 +38,4 @@
2738

2839
## 0.0.5-alpha - 2014-07-01
2940

30-
- Initial release
41+
- Initial release

src/Manipulation/JoinQuery.php

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function join(
101101
$select = QueryFactory::createSelect($table);
102102
$select->setColumns($columns);
103103
$select->setJoinType($joinType);
104+
$select->setParentQuery($this->select);
104105
$this->addJoin($select, $selfColumn, $refColumn);
105106
}
106107

src/Manipulation/Select.php

+41-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
1414
use NilPortugues\Sql\QueryBuilder\Syntax\Table;
1515
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
16+
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
1617

1718
/**
1819
* Class Select.
@@ -64,6 +65,11 @@ class Select extends AbstractBaseQuery
6465
*/
6566
protected $columnQuery;
6667

68+
/**
69+
* @var ParentQuery
70+
*/
71+
protected $parentQuery;
72+
6773
/**
6874
* @param string $table
6975
* @param array $columns
@@ -499,12 +505,42 @@ public function isDistinct()
499505
*/
500506
public function getAllOrderBy()
501507
{
502-
$order = $this->orderBy;
508+
return $this->orderBy;
509+
}
503510

504-
foreach ($this->joinQuery->getJoins() as $join) {
505-
$order = \array_merge($order, $join->getAllOrderBy());
506-
}
511+
/**
512+
* @return ParentQuery
513+
*/
514+
public function getParentQuery()
515+
{
516+
return $this->parentQuery;
517+
}
507518

508-
return $order;
519+
/**
520+
* @param Select $parentQuery
521+
*
522+
* @return $this
523+
*/
524+
public function setParentQuery(Select $parentQuery)
525+
{
526+
$this->parentQuery = $parentQuery;
527+
528+
return $this;
529+
}
530+
531+
/**
532+
* @param string $column
533+
* @param string $direction
534+
* @param null $table
535+
*
536+
* @return $this
537+
*/
538+
public function orderBy($column, $direction = OrderBy::ASC, $table = null)
539+
{
540+
$current = parent::orderBy($column, $direction, $table);
541+
if ($this->getParentQuery() != null) {
542+
$this->getParentQuery()->orderBy($column, $direction, \is_null($table) ? $this->getTable() : $table);
543+
}
544+
return $current;
509545
}
510546
}

tests/Manipulation/SelectTest.php

+63
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace NilPortugues\Tests\Sql\QueryBuilder\Manipulation;
1212

1313
use NilPortugues\Sql\QueryBuilder\Manipulation\Select;
14+
use NilPortugues\Sql\QueryBuilder\Syntax\OrderBy;
1415

1516
/**
1617
* Class SelectTest.
@@ -35,4 +36,66 @@ public function itShouldGetPartName()
3536
{
3637
$this->assertSame('SELECT', $this->query->partName());
3738
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function itShouldSetParentOrderByAlso()
44+
{
45+
$columns = [
46+
'id',
47+
'phase_id',
48+
'league_id',
49+
'date',
50+
];
51+
$parentTable = 'events';
52+
$this->query->setTable($parentTable);
53+
$this->query->setColumns($columns);
54+
55+
$sorts = [
56+
[
57+
'field' => 'league_id',
58+
'direction' => 1,
59+
],
60+
[
61+
'field' => 'start_date',
62+
'direction' => 0,
63+
'table' => 'phases',
64+
'joinBy' => 'phase_id',
65+
'joinWith' => 'id',
66+
],
67+
[
68+
'field' => 'date',
69+
'direction' => 1,
70+
],
71+
];
72+
73+
if (is_array($sorts)) {
74+
foreach ($sorts as $sort) {
75+
$order = (int)$sort['direction'] > 0 ? OrderBy::ASC : OrderBy::DESC;
76+
if (count($sort) == 5) {
77+
$this->query->leftJoin(
78+
$sort['table'],
79+
$sort['joinBy'],
80+
$sort['joinWith']
81+
)->orderBy($sort['field'], $order);
82+
} else {
83+
$this->query->orderBy($sort['field'], $order);
84+
}
85+
}
86+
}
87+
88+
$returnedOrders = $this->query->getAllOrderBy();
89+
foreach ($returnedOrders as $id => $orderByObject) {
90+
$column = $orderByObject->getColumn();
91+
$table = $column->getTable();
92+
$expectedColumn = $sorts[$id]['field'];
93+
$expectedTable = array_key_exists('table', $sorts[$id]) ? $sorts[$id]['table'] : $parentTable;
94+
$expectedDirection = (int)$sorts[$id]['direction'] > 0 ? OrderBy::ASC : OrderBy::DESC;
95+
$this->assertSame($expectedColumn, $column->getName());
96+
$this->assertSame($expectedTable, $table->getName());
97+
$this->assertSame($expectedDirection, $orderByObject->getDirection());
98+
}
99+
$this->assertCount(count($sorts), $returnedOrders);
100+
}
38101
}

0 commit comments

Comments
 (0)