Skip to content

Commit

Permalink
Fix owner and local key (#45)
Browse files Browse the repository at this point in the history
* iteration

* iteration2

* iteration3

* iteration5

* iteration8

* cs

* use owner and local keys + tests

* iteration

* iteration2

* iteration3

* iteration4

* iteration7

* finish

* typo
  • Loading branch information
fico7489 committed Jan 20, 2019
1 parent 0e000af commit 9db28c3
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 33 deletions.
11 changes: 7 additions & 4 deletions src/EloquentJoinBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,21 @@ private function performJoin($relations, $leftJoin = null)
if ($relatedRelation instanceof BelongsToJoin) {
$relatedKey = $relatedRelation->getQualifiedForeignKey();
$relatedKey = last(explode('.', $relatedKey));
$ownerKey = $relatedRelation->getOwnerKey();

$this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedKey, $currentTableAlias, $relatedPrimaryKey) {
$join->on($relatedTableAlias.'.'.$relatedPrimaryKey, '=', $currentTableAlias.'.'.$relatedKey);
$this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedKey, $currentTableAlias, $ownerKey) {
$join->on($relatedTableAlias.'.'.$ownerKey, '=', $currentTableAlias.'.'.$relatedKey);

$this->joinQuery($join, $relatedRelation, $relatedTableAlias);
});
} elseif ($relatedRelation instanceof HasOneJoin || $relatedRelation instanceof HasManyJoin) {
$relatedKey = $relatedRelation->getQualifiedForeignKeyName();
$relatedKey = last(explode('.', $relatedKey));
$localKey = $relatedRelation->getQualifiedParentKeyName();
$localKey = last(explode('.', $localKey));

$this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedKey, $currentTableAlias, $currentPrimaryKey) {
$join->on($relatedTableAlias.'.'.$relatedKey, '=', $currentTableAlias.'.'.$currentPrimaryKey);
$this->$joinMethod($joinQuery, function ($join) use ($relatedRelation, $relatedTableAlias, $relatedKey, $currentTableAlias, $currentPrimaryKey, $localKey) {
$join->on($relatedTableAlias.'.'.$relatedKey, '=', $currentTableAlias.'.'.$localKey);

$this->joinQuery($join, $relatedRelation, $relatedTableAlias);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Models/Key/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

class Location extends BaseModel
{
protected $primaryKey = 'key_id_location';
protected $primaryKey = 'id_location_primary';

protected $table = 'key_locations';

protected $fillable = ['address', 'seller_id'];
protected $fillable = ['address', 'id_seller_foreign'];
}
11 changes: 8 additions & 3 deletions tests/Models/Key/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@

class Order extends BaseModel
{
protected $primaryKey = 'key_id_order';
protected $primaryKey = 'id_order_primary';

protected $table = 'key_orders';

protected $fillable = ['number', 'seller_id'];
protected $fillable = ['number', 'id_seller_foreign'];

public function seller()
{
return $this->belongsTo(Seller::class, 'key_seller_id', 'key_id_order');
return $this->belongsTo(Seller::class, 'id_seller_foreign', 'id_seller_primary');
}

public function sellerOwner()
{
return $this->belongsTo(Seller::class, 'id_seller_foreign', 'id_seller_owner');
}
}
18 changes: 14 additions & 4 deletions tests/Models/Key/Seller.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@

class Seller extends BaseModel
{
protected $primaryKey = 'key_id_seller';
protected $primaryKey = 'id_seller_primary';

protected $table = 'key_sellers';

protected $fillable = ['title', 'deleted_at'];
protected $fillable = ['title'];

public function location()
{
return $this->hasOne(Location::class, 'key_seller_id', 'key_id_seller');
return $this->hasOne(Location::class, 'id_seller_foreign', 'id_seller_primary');
}

public function locations()
{
return $this->hasMany(Location::class, 'key_seller_id', 'key_id_seller');
return $this->hasMany(Location::class, 'id_seller_foreign', 'id_seller_primary');
}

public function locationOwner()
{
return $this->hasOne(Location::class, 'id_seller_foreign', 'id_seller_owner');
}

public function locationsOwner()
{
return $this->hasMany(Location::class, 'id_seller_foreign', 'id_seller_owner');
}
}
49 changes: 49 additions & 0 deletions tests/Tests/KeysOwnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Tests\Tests;

use Fico7489\Laravel\EloquentJoin\Tests\Models\Key\Order;
use Fico7489\Laravel\EloquentJoin\Tests\Models\Key\Seller;
use Fico7489\Laravel\EloquentJoin\Tests\TestCase;

class KeysOwnerTest extends TestCase
{
public function testBelogsTo()
{
Order::joinRelations('sellerOwner')
->get();

$queryTest = 'select key_orders.*
from "key_orders"
left join "key_sellers" on "key_sellers"."id_seller_owner" = "key_orders"."id_seller_foreign"
group by "key_orders"."id_order_primary"';

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

public function testHasOne()
{
Seller::joinRelations('locationOwner')
->get();

$queryTest = 'select key_sellers.*
from "key_sellers"
left join "key_locations" on "key_locations"."id_seller_foreign" = "key_sellers"."id_seller_owner"
group by "key_sellers"."id_seller_primary"';

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

public function testHasMany()
{
Seller::joinRelations('locationsOwner')
->get();

$queryTest = 'select key_sellers.*
from "key_sellers"
left join "key_locations" on "key_locations"."id_seller_foreign" = "key_sellers"."id_seller_owner"
group by "key_sellers"."id_seller_primary"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
}
22 changes: 11 additions & 11 deletions tests/Tests/KeysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

class KeysTest extends TestCase
{
public function testBelogsTo()
public function testBelongsTo()
{
Order::joinRelations('seller')
->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"';
left join "key_sellers" on "key_sellers"."id_seller_primary" = "key_orders"."id_seller_foreign"
group by "key_orders"."id_order_primary"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
Expand All @@ -26,10 +26,10 @@ 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"';
$queryTest = 'select key_sellers.*
from "key_sellers"
left join "key_locations" on "key_locations"."id_seller_foreign" = "key_sellers"."id_seller_primary"
group by "key_sellers"."id_seller_primary"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
Expand All @@ -39,10 +39,10 @@ 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"';
$queryTest = 'select key_sellers.*
from "key_sellers"
left join "key_locations" on "key_locations"."id_seller_foreign" = "key_sellers"."id_seller_primary"
group by "key_sellers"."id_seller_primary"';

$this->assertQueryMatches($queryTest, $this->fetchQuery());
}
Expand Down
22 changes: 13 additions & 9 deletions tests/database/migrations/2017_11_04_163552_create_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,30 @@ public function up()

//for key tests
Schema::create('key_orders', function (Blueprint $table) {
$table->increments('key_id_order');
$table->increments('id_order_primary');
$table->unsignedInteger('id_order_owner')->nullable();

$table->string('number')->nullable();
$table->unsignedInteger('key_seller_id')->nullable();

$table->foreign('key_seller_id')->references('id')->on('sellers');
$table->unsignedInteger('id_seller_foreign')->nullable();
$table->foreign('id_seller_foreign')->references('id')->on('sellers');
});

Schema::create('key_sellers', function (Blueprint $table) {
$table->increments('key_id_seller');
$table->increments('id_seller_primary');
$table->unsignedInteger('id_seller_owner')->nullable();

$table->string('title')->nullable();
$table->unsignedInteger('city_id')->nullable();
});

Schema::create('key_locations', function (Blueprint $table) {
$table->increments('id');
$table->increments('id_location_primary');
$table->unsignedInteger('id_location_owner')->nullable();

$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');
$table->unsignedInteger('id_seller_foreign')->nullable();
$table->foreign('id_seller_foreign')->references('id')->on('sellers');
});
}

Expand Down

0 comments on commit 9db28c3

Please sign in to comment.