Skip to content

Commit

Permalink
Fixes (#43)
Browse files Browse the repository at this point in the history
* iteration

* iteration2

* iteration3

* iteration4

* iteration5

* cs
  • Loading branch information
fico7489 authored Jan 19, 2019
1 parent 2fe6cf8 commit 0e000af
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/EloquentJoinBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,38 +134,35 @@ 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;
$relationAccumulatedString = implode('_', $relationsAccumulated);

//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);
});
} 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, $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);
});
Expand Down
14 changes: 14 additions & 0 deletions tests/Models/Key/Location.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Tests\Models\Key;

use Fico7489\Laravel\EloquentJoin\Tests\Models\BaseModel;

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

protected $table = 'key_locations';

protected $fillable = ['address', 'seller_id'];
}
19 changes: 19 additions & 0 deletions tests/Models/Key/Order.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Tests\Models\Key;

use Fico7489\Laravel\EloquentJoin\Tests\Models\BaseModel;

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

protected $table = 'key_orders';

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

public function seller()
{
return $this->belongsTo(Seller::class, 'key_seller_id', 'key_id_order');
}
}
24 changes: 24 additions & 0 deletions tests/Models/Key/Seller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Fico7489\Laravel\EloquentJoin\Tests\Models\Key;

use Fico7489\Laravel\EloquentJoin\Tests\Models\BaseModel;

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

protected $table = 'key_sellers';

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

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

public function locations()
{
return $this->hasMany(Location::class, 'key_seller_id', 'key_id_seller');
}
}
49 changes: 49 additions & 0 deletions tests/Tests/KeysTest.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 KeysTest extends TestCase
{
public function testBelogsTo()
{
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"';

$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());
}
}
30 changes: 30 additions & 0 deletions tests/database/migrations/2017_11_04_163552_create_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

/**
Expand All @@ -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');
}
}

0 comments on commit 0e000af

Please sign in to comment.