Skip to content

Commit

Permalink
add sort2, sort3 ...
Browse files Browse the repository at this point in the history
  • Loading branch information
fico7489 committed Jan 27, 2019
1 parent 9497090 commit 220aa02
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/EloquentJoinBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public function orderByJoin($column, $direction = 'asc', $aggregateMethod = null
if (false !== $dotPos) {
$aggregateMethod = $aggregateMethod ? $aggregateMethod : $this->aggregateMethod;
$this->checkAggregateMethod($aggregateMethod);
$sortAlias = 'sort'.count($this->query->orders ?? []);

$sortsCount = count($this->query->orders ?? []);
$sortAlias = 'sort'.(0 == $sortsCount ? '' : ($sortsCount + 1));

$query->selectRaw($aggregateMethod.'('.$column.') as '.$sortAlias);

return $this->orderByRaw($sortAlias.' '.$direction);
Expand Down
4 changes: 2 additions & 2 deletions tests/Tests/AggregateJoinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

class AggregateJoinTest extends TestCase
{
private $queryTest = 'select orders.*, SUM(sellers.id) as sort0
private $queryTest = 'select orders.*, SUM(sellers.id) as sort
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "orders"."deleted_at" is null
group by "orders"."id"
order by sort0 asc';
order by sort asc';

public function testAvg()
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Tests/Clauses/OrderByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@ public function testOrderBy()
->orderByJoin('seller.id', 'asc')
->get();

$queryTest = 'select orders.*, MAX(sellers.id) as sort0
$queryTest = 'select orders.*, MAX(sellers.id) as sort
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "orders"."deleted_at" is null
group by "orders"."id"
order by sort0 asc';
order by sort asc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}

public function testMultipleOrderBy()
public function testOrderByMultiple()
{
Order::joinRelations('seller')
->orderByJoin('seller.id', 'asc')
->orderByJoin('seller.title', 'desc')
->get();

$queryTest = 'select orders.*, MAX(sellers.id) as sort0, MAX(sellers.title) as sort1
$queryTest = 'select orders.*, MAX(sellers.id) as sort, MAX(sellers.title) as sort2
from "orders"
left join "sellers" on "sellers"."id" = "orders"."seller_id"
where "orders"."deleted_at" is null
group by "orders"."id"
order by sort0 asc, sort1 desc';
order by sort asc, sort2 desc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
Expand Down
16 changes: 8 additions & 8 deletions tests/Tests/ClosureOnRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,51 @@ public function testWhereOnRelationWithOrderByJoin()
{
//location have two where ['is_primary => 0', 'is_secondary' => 0]
$items = Seller::orderByJoin('location.id', 'desc')->get();
$queryTest = 'select sellers.*, MAX(locations.id) as sort0 from "sellers"
$queryTest = 'select sellers.*, MAX(locations.id) as sort from "sellers"
left join "locations"
on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."is_secondary" = ?
and "locations"."deleted_at" is null
group by "sellers"."id"
order by sort0 desc';
order by sort desc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());

//locationPrimary have one where ['is_primary => 1']
$items = Seller::orderByJoin('locationPrimary.id', 'desc')->get();
$queryTest = 'select sellers.*, MAX(locations.id) as sort0 from "sellers"
$queryTest = 'select sellers.*, MAX(locations.id) as sort from "sellers"
left join "locations"
on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
and "locations"."deleted_at" is null
group by "sellers"."id"
order by sort0 desc';
order by sort desc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());

//locationPrimary have one where ['is_secondary => 1']
$items = Seller::orderByJoin('locationSecondary.id', 'desc')->get();
$queryTest = 'select sellers.*, MAX(locations.id) as sort0 from "sellers"
$queryTest = 'select sellers.*, MAX(locations.id) as sort from "sellers"
left join "locations"
on "locations"."seller_id" = "sellers"."id"
and "locations"."is_secondary" = ?
and "locations"."deleted_at" is null
group by "sellers"."id"
order by sort0 desc';
order by sort desc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());

//locationPrimary have one where ['is_primary => 1'] and one orWhere ['is_secondary => 1']
$items = Seller::orderByJoin('locationPrimaryOrSecondary.id', 'desc')->get();
$queryTest = 'select sellers.*, MAX(locations.id) as sort0 from "sellers"
$queryTest = 'select sellers.*, MAX(locations.id) as sort from "sellers"
left join "locations"
on "locations"."seller_id" = "sellers"."id"
and "locations"."is_primary" = ?
or "locations"."is_secondary" = ?
and "locations"."deleted_at" is null
group by "sellers"."id"
order by sort0 desc';
order by sort desc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
Expand Down
24 changes: 12 additions & 12 deletions tests/Tests/SoftDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,86 +53,86 @@ public function testNotRelatedWithTrashedExplicit()
public function testRelatedWithoutTrashedDefault()
{
OrderItem::orderByJoin('order.number')->get();
$queryTest = 'select order_items.*, MAX(orders.number) as sort0
$queryTest = 'select order_items.*, MAX(orders.number) as sort
from "order_items" left join "orders"
on "orders"."id" = "order_items"."order_id"
and "orders"."deleted_at" is null
where "order_items"."deleted_at" is null
group by "order_items"."id"
order by sort0 asc';
order by sort asc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}

public function testRelatedWithoutTrashedExplicit()
{
OrderItem::orderByJoin('order.number')->withoutTrashed()->get();
$queryTest = 'select order_items.*, MAX(orders.number) as sort0
$queryTest = 'select order_items.*, MAX(orders.number) as sort
from "order_items"
left join "orders"
on "orders"."id" = "order_items"."order_id"
and "orders"."deleted_at" is null
where "order_items"."deleted_at" is null
group by "order_items"."id"
order by sort0 asc';
order by sort asc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}

public function testRelatedOnlyTrashedExplicit()
{
OrderItem::orderByJoin('order.number')->onlyTrashed()->get();
$queryTest = 'select order_items.*, MAX(orders.number) as sort0
$queryTest = 'select order_items.*, MAX(orders.number) as sort
from "order_items"
left join "orders"
on "orders"."id" = "order_items"."order_id"
and "orders"."deleted_at" is null
where "order_items"."deleted_at" is not null
group by "order_items"."id"
order by sort0 asc';
order by sort asc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}

public function testRelatedWithTrashedExplicit()
{
OrderItem::orderByJoin('order.number')->withTrashed()->get();
$queryTest = 'select order_items.*, MAX(orders.number) as sort0
$queryTest = 'select order_items.*, MAX(orders.number) as sort
from "order_items"
left join "orders"
on "orders"."id" = "order_items"."order_id"
and "orders"."deleted_at" is null
group by "order_items"."id"
order by sort0 asc';
order by sort asc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}

public function testRelatedWithTrashedOnRelation()
{
OrderItem::orderByJoin('orderWithTrashed.number')->get();
$queryTest = 'select order_items.*, MAX(orders.number) as sort0
$queryTest = 'select order_items.*, MAX(orders.number) as sort
from "order_items"
left join "orders"
on "orders"."id" = "order_items"."order_id"
where "order_items"."deleted_at" is null
group by "order_items"."id"
order by sort0 asc';
order by sort asc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}

public function testRelatedOnlyTrashedOnRelation()
{
OrderItem::orderByJoin('orderOnlyTrashed.number')->get();
$queryTest = 'select order_items.*, MAX(orders.number) as sort0
$queryTest = 'select order_items.*, MAX(orders.number) as sort
from "order_items"
left join "orders"
on "orders"."id" = "order_items"."order_id"
and "orders"."deleted_at" is not null
where "order_items"."deleted_at" is null
group by "order_items"."id"
order by sort0 asc';
order by sort asc';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
Expand Down

0 comments on commit 220aa02

Please sign in to comment.