Skip to content

Commit

Permalink
Merge pull request #105 from tsterker/fix-has-many-through
Browse files Browse the repository at this point in the history
fix: HasManyThrough and HasOneThrough not working
  • Loading branch information
calebporzio authored Nov 16, 2022
2 parents c4f458f + f304838 commit 5511ee8
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function bootHasParent(): void
$instance = new static;

if ($instance->parentHasHasChildrenTrait()) {
$query->where($instance->getTable().'.'.$instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance)));
$query->where($query->getModel()->getTable().'.'.$instance->getInheritanceColumn(), $instance->classToAlias(get_class($instance)));
}
});
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Features/ChildModelsAreAutomaticallyScopedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Parental\Tests\Models\Admin;
use Parental\Tests\Models\Car;
use Parental\Tests\Models\ChildNode;
use Parental\Tests\Models\Driver;
use Parental\Tests\Models\NodeEdge;
use Parental\Tests\Models\ParentNode;
use Parental\Tests\Models\Passenger;
use Parental\Tests\Models\Trip;
use Parental\Tests\Models\User;
Expand Down Expand Up @@ -71,4 +74,57 @@ function child_is_scoped_when_accessed_from_belongs_to_many()
$this->assertCount(1, $trip->cars);
$this->assertCount(2, $trip->vehicles);
}

/** @test */
function child_is_scoped_when_accessed_from_has_one_through()
{
// Create root with children
$rootA = ParentNode::create(['name' => 'Root A']);
$childA = ChildNode::create(['name' => 'Child 1']);
$childB = ChildNode::create(['name' => 'Child 2']);
$childC = ChildNode::create(['name' => 'Child 3']);
NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childA->id]);
NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childB->id]);

$this->assertInstanceOf(ParentNode::class, $childA->parent);
$this->assertInstanceOf(ParentNode::class, $childB->parent);
$this->assertNull($childC->parent);

$this->assertCount(2, ChildNode::whereHas('parent')->get());
$this->assertTrue(ChildNode::whereId($childA->id)->whereHas('parent')->exists());
$this->assertTrue(ChildNode::whereId($childB->id)->whereHas('parent')->exists());
$this->assertFalse(ChildNode::whereId($childC->id)->whereHas('parent')->exists());
}

/** @test */
function child_is_scoped_when_accessed_from_has_many_through()
{
// Create root with children
$rootA = ParentNode::create(['name' => 'Root A']);
$childA = ChildNode::create(['name' => 'Child 1']);
$childB = ChildNode::create(['name' => 'Child 2']);
$childC = ChildNode::create(['name' => 'Child 3']);
NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childA->id]);
NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childB->id]);
NodeEdge::create(['parent_node_id' => $rootA->id, 'child_node_id' => $childC->id]);

// Create different root with different children
$rootB = ParentNode::create(['name' => 'Root B']);
$childX = ChildNode::create(['name' => 'Child X']);
NodeEdge::create(['parent_node_id' => $rootB->id, 'child_node_id' => $childX->id]);

// Create different root children any children
$rootC = ParentNode::create(['name' => 'Root C']);

$this->assertCount(3, $rootA->children);
$this->assertContainsOnlyInstancesOf(ChildNode::class, $rootA->children);

$this->assertCount(1, $rootB->children);
$this->assertContainsOnlyInstancesOf(ChildNode::class, $rootB->children);

$this->assertCount(2, ParentNode::whereHas('children')->get());
$this->assertTrue(ParentNode::whereId($rootA->id)->whereHas('children')->exists());
$this->assertTrue(ParentNode::whereId($rootB->id)->whereHas('children')->exists());
$this->assertFalse(ParentNode::whereId($rootC->id)->whereHas('children')->exists());
}
}
24 changes: 24 additions & 0 deletions tests/Models/ChildNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Parental\Tests\Models;

use Parental\HasParent;

class ChildNode extends Node
{
use HasParent;

protected $guarded = [];

public function parent()
{
return $this->hasOneThrough(
ParentNode::class,
NodeEdge::class,
'child_node_id',
'id',
'id',
'parent_node_id',
);
}
}
13 changes: 13 additions & 0 deletions tests/Models/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Parental\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Parental\HasChildren;

class Node extends Model
{
use HasChildren;

protected $guarded = [];
}
10 changes: 10 additions & 0 deletions tests/Models/NodeEdge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Parental\Tests\Models;

use Illuminate\Database\Eloquent\Model;

class NodeEdge extends Model
{
protected $guarded = [];
}
24 changes: 24 additions & 0 deletions tests/Models/ParentNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Parental\Tests\Models;

use Parental\HasParent;

class ParentNode extends Node
{
use HasParent;

protected $guarded = [];

public function children()
{
return $this->hasManyThrough(
ChildNode::class,
NodeEdge::class,
'parent_node_id',
'id',
'id',
'child_node_id',
);
}
}
16 changes: 16 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Parental\Tests\Models\LeCredential;
use Parental\Tests\Models\OorCredential;

class TestCase extends BaseTestCase
{
Expand Down Expand Up @@ -82,5 +84,19 @@ public function runMigrations()
$table->morphs('partable');
$table->timestamps();
});

Schema::create('nodes', function ($table) {
$table->increments('id');
$table->string('type');
$table->string('name');
$table->timestamps();
});

Schema::create('node_edges', function ($table) {
$table->increments('id');
$table->string('parent_node_id');
$table->string('child_node_id');
$table->timestamps();
});
}
}

0 comments on commit 5511ee8

Please sign in to comment.