Skip to content

Commit 74b9969

Browse files
authored
Merge pull request #6 from AuroraWebSoftware/added-ignore-scopes-in-connectives-func
Added ignore scopes in connectives func
2 parents 0764c06 + abfe7fb commit 74b9969

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

src/Traits/Connective.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function connections(string|array|null $connectionTypes = null, string|ar
7777
/**
7878
* @return ConnectiveCollection<ConnectiveContract>|null
7979
*/
80-
public function connectives(string|array|null $connectionTypes = null, string|array|null $modelTypes = null): ?ConnectiveCollection
80+
public function connectives(string|array|null $connectionTypes = null, string|array|null $modelTypes = null, ?array $ignoreScopes = []): ?ConnectiveCollection
8181
{
8282
$connections = $this->connections($connectionTypes, $modelTypes);
8383
$collection = ConnectiveCollection::make();
@@ -86,8 +86,16 @@ public function connectives(string|array|null $connectionTypes = null, string|ar
8686
$toModelType = $connection->to_model_type;
8787
$toModelId = $connection->to_model_id;
8888

89-
$toModelInstance = $toModelType::find($toModelId);
90-
$collection->push($toModelInstance);
89+
if ($ignoreScopes && is_array($ignoreScopes)) {
90+
$toModelInstance = $toModelType::withoutGlobalScopes($ignoreScopes)->find($toModelId);
91+
} else {
92+
$toModelInstance = $toModelType::find($toModelId);
93+
}
94+
95+
if ($toModelInstance != null) {
96+
$collection->push($toModelInstance);
97+
}
98+
9199
}
92100

93101
return $collection;
@@ -122,7 +130,7 @@ public function inverseConnections(string|array|null $connectionTypes = null, st
122130
/**
123131
* @return ConnectiveCollection<ConnectiveContract>|null
124132
*/
125-
public function inverseConnectives(string|array|null $connectionTypes = null, string|array|null $modelTypes = null): ?ConnectiveCollection
133+
public function inverseConnectives(string|array|null $connectionTypes = null, string|array|null $modelTypes = null, ?array $ignoreScopes = []): ?ConnectiveCollection
126134
{
127135
$incomingConnections = $this->inverseConnections($connectionTypes, $modelTypes);
128136
$collection = ConnectiveCollection::make();
@@ -131,8 +139,16 @@ public function inverseConnectives(string|array|null $connectionTypes = null, st
131139
$fromModelType = $incomingConnection->from_model_type;
132140
$fromModelId = $incomingConnection->from_model_id;
133141

134-
$fromModelInstance = $fromModelType::find($fromModelId);
135-
$collection->push($fromModelInstance);
142+
if ($ignoreScopes && is_array($ignoreScopes)) {
143+
$fromModelInstance = $fromModelType::withoutGlobalScopes($ignoreScopes)->find($fromModelId);
144+
} else {
145+
$fromModelInstance = $fromModelType::find($fromModelId);
146+
}
147+
148+
if ($fromModelInstance != null) {
149+
$collection->push($fromModelInstance);
150+
}
151+
136152
}
137153

138154
return $collection;

tests/PackageTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,40 @@
360360

361361
});
362362

363+
it('can get connectives some scopes can be excluded in a model that', function () {
364+
365+
/**
366+
* @var ConnectiveContract & \AuroraWebSoftware\Connective\Tests\Models\Connective $connective2
367+
*/
368+
$connective2 = \AuroraWebSoftware\Connective\Tests\Models\Connective::create([
369+
'name' => 'name72',
370+
]);
371+
372+
/**
373+
* @var ConnectiveContract & \AuroraWebSoftware\Connective\Tests\Models\Connective $connective3
374+
*/
375+
$connective3 = \AuroraWebSoftware\Connective\Tests\Models\Connective::create([
376+
'name' => 'name73',
377+
]);
378+
379+
/**
380+
* @var ConnectiveContract & \AuroraWebSoftware\Connective\Tests\Models\Connective $connective4
381+
*/
382+
$connective4 = \AuroraWebSoftware\Connective\Tests\Models\Connective::create([
383+
'name' => 'name74',
384+
]);
385+
386+
\AuroraWebSoftware\Connective\Tests\Models\Connective::addGlobalScope('name', function (\Illuminate\Database\Eloquent\Builder $builder) {
387+
$builder->where('name', 'name73');
388+
});
389+
390+
$connective2->connectTo($connective3, 'a');
391+
$connective2->connectTo($connective4, 'a');
392+
393+
expect($connective2->connectives('a'))->toHaveCount(1);
394+
expect($connective2->connectives('a', null, ['name']))->toHaveCount(2);
395+
});
396+
363397
it('can get inverse connections of a connective model', function () {
364398

365399
/**

0 commit comments

Comments
 (0)