From 220aa02b973055f40885df6b00ddec47e68e43cb Mon Sep 17 00:00:00 2001 From: Filip Horvat Date: Sun, 27 Jan 2019 11:54:41 +0100 Subject: [PATCH] add sort2, sort3 ... --- src/EloquentJoinBuilder.php | 5 ++++- tests/Tests/AggregateJoinTest.php | 4 ++-- tests/Tests/Clauses/OrderByTest.php | 10 +++++----- tests/Tests/ClosureOnRelationTest.php | 16 ++++++++-------- tests/Tests/SoftDeleteTest.php | 24 ++++++++++++------------ 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/EloquentJoinBuilder.php b/src/EloquentJoinBuilder.php index 6592d6b..bd15f96 100644 --- a/src/EloquentJoinBuilder.php +++ b/src/EloquentJoinBuilder.php @@ -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); diff --git a/tests/Tests/AggregateJoinTest.php b/tests/Tests/AggregateJoinTest.php index d3c112f..2510181 100644 --- a/tests/Tests/AggregateJoinTest.php +++ b/tests/Tests/AggregateJoinTest.php @@ -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() { diff --git a/tests/Tests/Clauses/OrderByTest.php b/tests/Tests/Clauses/OrderByTest.php index db853c3..9ecbc97 100644 --- a/tests/Tests/Clauses/OrderByTest.php +++ b/tests/Tests/Clauses/OrderByTest.php @@ -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()); } diff --git a/tests/Tests/ClosureOnRelationTest.php b/tests/Tests/ClosureOnRelationTest.php index d1f6c40..1a51a19 100644 --- a/tests/Tests/ClosureOnRelationTest.php +++ b/tests/Tests/ClosureOnRelationTest.php @@ -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()); } diff --git a/tests/Tests/SoftDeleteTest.php b/tests/Tests/SoftDeleteTest.php index 60de164..aeff8e6 100644 --- a/tests/Tests/SoftDeleteTest.php +++ b/tests/Tests/SoftDeleteTest.php @@ -53,13 +53,13 @@ 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()); } @@ -67,14 +67,14 @@ public function testRelatedWithoutTrashedDefault() 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()); } @@ -82,14 +82,14 @@ public function testRelatedWithoutTrashedExplicit() 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()); } @@ -97,13 +97,13 @@ public function testRelatedOnlyTrashedExplicit() 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()); } @@ -111,13 +111,13 @@ public function testRelatedWithTrashedExplicit() 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()); } @@ -125,14 +125,14 @@ public function testRelatedWithTrashedOnRelation() 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()); }