Skip to content

Commit bea240b

Browse files
committed
fix through relation plus tests
1 parent 60ca5ae commit bea240b

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

docs/relations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- [One of many](#one-of-many)
77
- [Has one through](#has-one-through)
88
- [Has many through](#has-many-through)
9+
- [Many to many](#many-to-many)
910

1011
## One to one
1112

src/Relation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ public function applyRelation(array $id, string $primaryKey): static
142142
sprintf(
143143
'%s.%s = %s.%s',
144144
$this->through->model->getTable(),
145-
$this->through->foreignKey,
145+
$this->through->primaryKey,
146146
$this->model->getTable(),
147-
$this->primaryKey
147+
$this->foreignKey
148148
),
149149
'LEFT'
150150
)

tests/ThroughTest.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use CodeIgniter\Test\DatabaseTestTrait;
99
use Tests\Support\Database\Seeds\SeedTests;
1010
use Tests\Support\Entities\Address;
11+
use Tests\Support\Entities\Country;
1112
use Tests\Support\Entities\Post;
1213
use Tests\Support\Entities\User;
14+
use Tests\Support\Models\CountryModel;
1315
use Tests\Support\Models\UserModel;
1416

1517
/**
@@ -56,31 +58,31 @@ public function testFindAllHasOneThrough()
5658

5759
public function testFindHasManyThrough()
5860
{
59-
$user = model(UserModel::class)->with('posts')->find(1);
60-
$this->assertInstanceOf(User::class, $user);
61+
$country = model(CountryModel::class)->with('posts')->find(1);
62+
$this->assertInstanceOf(Country::class, $country);
6163

62-
$isset = isset($user->posts);
64+
$isset = isset($country->posts);
6365
$this->assertTrue($isset);
6466

65-
$this->assertCount(5, $user->posts);
67+
$this->assertCount(5, $country->posts);
6668

67-
$this->assertInstanceOf(Post::class, $user->posts[0]);
69+
$this->assertInstanceOf(Post::class, $country->posts[0]);
6870
}
6971

7072
public function testFindAllHasManyThrough()
7173
{
72-
$users = model(UserModel::class)->with('posts')->findAll();
73-
$this->assertInstanceOf(User::class, $users[0]);
74+
$countries = model(CountryModel::class)->with('posts')->findAll();
75+
$this->assertInstanceOf(Country::class, $countries[0]);
7476

75-
$this->assertCount(2, $users);
77+
$this->assertCount(2, $countries);
7678

77-
$isset = isset($users[0]->posts);
79+
$isset = isset($countries[0]->posts);
7880
$this->assertTrue($isset);
7981

80-
$this->assertCount(5, $users[0]->posts);
81-
$this->assertInstanceOf(Post::class, $users[0]->posts[0]);
82+
$this->assertCount(5, $countries[0]->posts);
83+
$this->assertInstanceOf(Post::class, $countries[0]->posts[0]);
8284

83-
$this->assertCount(3, $users[1]->posts);
84-
$this->assertInstanceOf(Post::class, $users[0]->posts[1]);
85+
$this->assertCount(3, $countries[1]->posts);
86+
$this->assertInstanceOf(Post::class, $countries[0]->posts[1]);
8587
}
8688
}

0 commit comments

Comments
 (0)