Skip to content

Commit a0fd6b3

Browse files
committed
optimizing the depth of relationships
1 parent 95f6bbc commit a0fd6b3

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

src/Traits/HasWallets.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ public function getWalletOrFail(string $slug): WalletModel
6262
if ($this->_wallets === [] && $this->relationLoaded('wallets')) {
6363
/** @var WalletModel $wallet */
6464
foreach ($this->getRelation('wallets') as $wallet) {
65-
$wallet->setRelation('holder', $this);
65+
$wallet->setRelation('holder', $this->withoutRelations());
6666
$this->_wallets[$wallet->slug] = $wallet;
6767
}
6868
}
6969

7070
if (!array_key_exists($slug, $this->_wallets)) {
7171
$wallet = app(WalletServiceInterface::class)->getBySlug($this, $slug);
72-
$wallet->setRelation('holder', $this);
72+
$wallet->setRelation('holder', $this->withoutRelations());
7373

7474
$this->_wallets[$slug] = $wallet;
7575
}
@@ -89,7 +89,7 @@ public function createWallet(array $data): WalletModel
8989
{
9090
$wallet = app(WalletServiceInterface::class)->create($this, $data);
9191
$this->_wallets[$wallet->slug] = $wallet;
92-
$wallet->setRelation('holder', $this);
92+
$wallet->setRelation('holder', $this->withoutRelations());
9393

9494
return $wallet;
9595
}

src/Traits/MorphOneWallet.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ public function wallet(): MorphOne
3434
]));
3535

3636
if (property_exists($holder, 'exists') && $holder->exists) {
37-
$wallet->setRelation('holder', $holder);
37+
$wallet->setRelation(
38+
'holder',
39+
method_exists($holder, 'withoutRelations')
40+
? $holder->withoutRelations()
41+
: $holder
42+
);
3843
}
3944
})
4045
;

tests/Units/Domain/EagerLoadingTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ public function testTransferTransactions(): void
6262
self::assertTrue($transfer->relationLoaded('from'));
6363
self::assertTrue($transfer->relationLoaded('to'));
6464

65-
self::assertSame($user1->wallet->getMorphClass(), $transfer->from->getMorphClass());
66-
self::assertSame($user1->wallet->getKey(), $transfer->from->getKey());
67-
self::assertSame($user2->wallet->getMorphClass(), $transfer->to->getMorphClass());
68-
self::assertSame($user2->wallet->getKey(), $transfer->to->getKey());
65+
self::assertTrue($user1->wallet->is($transfer->from));
66+
self::assertTrue($user2->wallet->is($transfer->to));
6967
}
7068

7169
public function testMultiWallets(): void
@@ -86,6 +84,6 @@ public function testMultiWallets(): void
8684
self::assertNotNull($user->getWallet('hello'));
8785
self::assertNotNull($user->getWallet('world'));
8886
self::assertTrue($user->getWallet('hello')->relationLoaded('holder'));
89-
self::assertSame($user, $user->getWallet('hello')->holder);
87+
self::assertTrue($user->is($user->getWallet('hello')->holder));
9088
}
9189
}

0 commit comments

Comments
 (0)