diff --git a/src/EloquentJoinBuilder.php b/src/EloquentJoinBuilder.php index 53b0d3c..6d88feb 100644 --- a/src/EloquentJoinBuilder.php +++ b/src/EloquentJoinBuilder.php @@ -134,8 +134,8 @@ private function performJoin($relations, $leftJoin = null) /** @var Relation $relatedRelation */ $relatedRelation = $currentModel->$relation(); $relatedModel = $relatedRelation->getRelated(); - $relatedTable = $relatedModel->getTable(); $relatedPrimaryKey = $relatedModel->getKeyName(); + $relatedTable = $relatedModel->getTable(); $relatedTableAlias = $this->useTableAlias ? uniqid() : $relatedTable; $relationsAccumulated[] = $relatedTableAlias; @@ -143,17 +143,17 @@ private function performJoin($relations, $leftJoin = null) //relations count if ($this->appendRelationsCount) { - $this->selectRaw('COUNT('.$relatedTable.'.'.$relatedPrimaryKey.') as '.$relationAccumulatedString.'_count'); + $this->selectRaw('COUNT('.$relatedTableAlias.'.'.$relatedPrimaryKey.') as '.$relationAccumulatedString.'_count'); } if (!in_array($relationAccumulatedString, $this->joinedTables)) { - $joinQuery = $relatedTable.($this->useTableAlias ? ' as '.$relatedTableAlias : ''); + $joinQuery = $relatedTableAlias.($this->useTableAlias ? ' as '.$relatedTableAlias : ''); if ($relatedRelation instanceof BelongsToJoin) { - $relatedKey = $relatedRelation->getForeignKey(); - $ownerKey = $relatedRelation->getOwnerKey(); + $relatedKey = $relatedRelation->getQualifiedForeignKey(); + $relatedKey = last(explode('.', $relatedKey)); - $this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $ownerKey, $currentTableAlias, $relatedKey) { - $join->on($relatedTableAlias.'.'.$ownerKey, '=', $currentTableAlias.'.'.$relatedKey); + $this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedKey, $currentTableAlias, $relatedPrimaryKey) { + $join->on($relatedTableAlias.'.'.$relatedPrimaryKey, '=', $currentTableAlias.'.'.$relatedKey); $this->joinQuery($join, $relatedRelation, $relatedTableAlias); }); @@ -161,11 +161,8 @@ private function performJoin($relations, $leftJoin = null) $relatedKey = $relatedRelation->getQualifiedForeignKeyName(); $relatedKey = last(explode('.', $relatedKey)); - $localKey = $relatedRelation->getQualifiedParentKeyName(); - $localKey = last(explode('.', $localKey)); - - $this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedKey, $currentTableAlias, $localKey) { - $join->on($relatedTableAlias.'.'.$relatedKey, '=', $currentTableAlias.'.'.$localKey); + $this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedKey, $currentTableAlias, $currentPrimaryKey) { + $join->on($relatedTableAlias.'.'.$relatedKey, '=', $currentTableAlias.'.'.$currentPrimaryKey); $this->joinQuery($join, $relatedRelation, $relatedTableAlias); }); diff --git a/tests/Models/Key/Location.php b/tests/Models/Key/Location.php new file mode 100644 index 0000000..9b6094c --- /dev/null +++ b/tests/Models/Key/Location.php @@ -0,0 +1,14 @@ +belongsTo(Seller::class, 'key_seller_id', 'key_id_order'); + } +} diff --git a/tests/Models/Key/Seller.php b/tests/Models/Key/Seller.php new file mode 100644 index 0000000..24db010 --- /dev/null +++ b/tests/Models/Key/Seller.php @@ -0,0 +1,24 @@ +hasOne(Location::class, 'key_seller_id', 'key_id_seller'); + } + + public function locations() + { + return $this->hasMany(Location::class, 'key_seller_id', 'key_id_seller'); + } +} diff --git a/tests/Tests/KeysTest.php b/tests/Tests/KeysTest.php new file mode 100644 index 0000000..7cdaa92 --- /dev/null +++ b/tests/Tests/KeysTest.php @@ -0,0 +1,49 @@ +get(); + + $queryTest = 'select key_orders.* + from "key_orders" + left join "key_sellers" on "key_sellers"."key_id_seller" = "key_orders"."key_seller_id" + group by "key_orders"."key_id_order"'; + + $this->assertQueryMatches($queryTest, $this->fetchQuery()); + } + + public function testHasOne() + { + Seller::joinRelations('location') + ->get(); + + $queryTest = 'select key_sellers.* + from "key_sellers" + left join "key_locations" on "key_locations"."key_seller_id" = "key_sellers"."key_id_seller" + group by "key_sellers"."key_id_seller"'; + + $this->assertQueryMatches($queryTest, $this->fetchQuery()); + } + + public function testHasMany() + { + Seller::joinRelations('locations') + ->get(); + + $queryTest = 'select key_sellers.* + from "key_sellers" + left join "key_locations" on "key_locations"."key_seller_id" = "key_sellers"."key_id_seller" + group by "key_sellers"."key_id_seller"'; + + $this->assertQueryMatches($queryTest, $this->fetchQuery()); + } +} diff --git a/tests/database/migrations/2017_11_04_163552_create_database.php b/tests/database/migrations/2017_11_04_163552_create_database.php index d7a4813..696abce 100644 --- a/tests/database/migrations/2017_11_04_163552_create_database.php +++ b/tests/database/migrations/2017_11_04_163552_create_database.php @@ -125,6 +125,30 @@ public function up() $table->timestamps(); $table->softDeletes(); }); + + //for key tests + Schema::create('key_orders', function (Blueprint $table) { + $table->increments('key_id_order'); + $table->string('number')->nullable(); + $table->unsignedInteger('key_seller_id')->nullable(); + + $table->foreign('key_seller_id')->references('id')->on('sellers'); + }); + + Schema::create('key_sellers', function (Blueprint $table) { + $table->increments('key_id_seller'); + $table->string('title')->nullable(); + $table->unsignedInteger('city_id')->nullable(); + }); + + Schema::create('key_locations', function (Blueprint $table) { + $table->increments('id'); + $table->string('address')->nullable(); + $table->boolean('is_primary')->default(0); + $table->unsignedInteger('key_seller_id')->nullable(); + + $table->foreign('key_seller_id')->references('id')->on('sellers'); + }); } /** @@ -141,5 +165,11 @@ public function down() Schema::drop('states'); Schema::drop('location_addresses'); Schema::drop('integrations'); + Schema::drop('orders'); + + //for key tests + Schema::drop('key_orders'); + Schema::drop('key_sellers'); + Schema::drop('key_locations'); } }