Skip to content

Commit

Permalink
optimizing the depth of relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Apr 26, 2022
1 parent 95f6bbc commit a0fd6b3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Traits/HasWallets.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public function getWalletOrFail(string $slug): WalletModel
if ($this->_wallets === [] && $this->relationLoaded('wallets')) {
/** @var WalletModel $wallet */
foreach ($this->getRelation('wallets') as $wallet) {
$wallet->setRelation('holder', $this);
$wallet->setRelation('holder', $this->withoutRelations());
$this->_wallets[$wallet->slug] = $wallet;
}
}

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

$this->_wallets[$slug] = $wallet;
}
Expand All @@ -89,7 +89,7 @@ public function createWallet(array $data): WalletModel
{
$wallet = app(WalletServiceInterface::class)->create($this, $data);
$this->_wallets[$wallet->slug] = $wallet;
$wallet->setRelation('holder', $this);
$wallet->setRelation('holder', $this->withoutRelations());

return $wallet;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Traits/MorphOneWallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public function wallet(): MorphOne
]));

if (property_exists($holder, 'exists') && $holder->exists) {
$wallet->setRelation('holder', $holder);
$wallet->setRelation(
'holder',
method_exists($holder, 'withoutRelations')
? $holder->withoutRelations()
: $holder
);
}
})
;
Expand Down
8 changes: 3 additions & 5 deletions tests/Units/Domain/EagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ public function testTransferTransactions(): void
self::assertTrue($transfer->relationLoaded('from'));
self::assertTrue($transfer->relationLoaded('to'));

self::assertSame($user1->wallet->getMorphClass(), $transfer->from->getMorphClass());
self::assertSame($user1->wallet->getKey(), $transfer->from->getKey());
self::assertSame($user2->wallet->getMorphClass(), $transfer->to->getMorphClass());
self::assertSame($user2->wallet->getKey(), $transfer->to->getKey());
self::assertTrue($user1->wallet->is($transfer->from));
self::assertTrue($user2->wallet->is($transfer->to));
}

public function testMultiWallets(): void
Expand All @@ -86,6 +84,6 @@ public function testMultiWallets(): void
self::assertNotNull($user->getWallet('hello'));
self::assertNotNull($user->getWallet('world'));
self::assertTrue($user->getWallet('hello')->relationLoaded('holder'));
self::assertSame($user, $user->getWallet('hello')->holder);
self::assertTrue($user->is($user->getWallet('hello')->holder));
}
}

0 comments on commit a0fd6b3

Please sign in to comment.