Skip to content

Commit

Permalink
Merge branch '1.7'
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
  • Loading branch information
staudenmeir committed Jun 20, 2023
2 parents d66ae5b + 0eb283b commit 88919ad
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"require-dev": {
"phpunit/phpunit": "^10.1",
"staudenmeir/eloquent-has-many-deep": "^1.18"
"staudenmeir/eloquent-has-many-deep": "^1.18.1"
},
"autoload": {
"psr-4": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ public function appendToDeepRelationship(array $through, array $foreignKeys, arr
* @param array $models
* @param \Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @param string $type
* @return array
*/
public function matchResultsForDeepRelationship(array $models, Collection $results, string $relation): array
{
public function matchResultsForDeepRelationship(
array $models,
Collection $results,
string $relation,
string $type = 'many'
): array {
$dictionary = $this->buildDictionaryForDeepRelationship($results);

foreach ($models as $model) {
Expand All @@ -73,9 +78,11 @@ public function matchResultsForDeepRelationship(array $models, Collection $resul
}
}

$collection = $this->related->newCollection($matches);
$value = $type === 'one'
? (reset($matches) ?: null)
: $this->related->newCollection($matches);

$model->setRelation($relation, $collection);
$model->setRelation($relation, $value);
}

return $models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,28 @@ public function getThroughKeyForDeepRelationships(string $alias): Expression
* @param array $models
* @param \Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @param string $type
* @return array
*/
public function matchResultsForDeepRelationship(array $models, Collection $results, string $relation): array
{
public function matchResultsForDeepRelationship(
array $models,
Collection $results,
string $relation,
string $type = 'many'
): array {
$dictionary = $this->buildDictionaryForDeepRelationship($results);

foreach ($models as $model) {
$key = $this->getDictionaryKey($model->{$this->localKey});

if (isset($dictionary[$key])) {
$collection = $this->related->newCollection($dictionary[$key]);
$value = $dictionary[$key];

$value = $type === 'one'
? (reset($value) ?: null)
: $this->related->newCollection($value);

$model->setRelation($relation, $collection);
$model->setRelation($relation, $value);
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Concatenation/BelongsToJson/FirstPositionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public function testEagerLoading()
$this->assertEquals([83, 84], $users[2]->permissions->pluck('id')->all());
}

public function testEagerLoadingWithHasOneDeep()
{
$users = User::with('permission')->get();

$this->assertEquals(81, $users[0]->permission->id);
$this->assertNull($users[1]->permission);
$this->assertEquals(83, $users[2]->permission->id);
}

public function testEagerLoadingWithObjects()
{
$users = User::with('permissions2')->get();
Expand Down
9 changes: 9 additions & 0 deletions tests/Concatenation/HasManyJson/FirstPositionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public function testEagerLoadingWithObjects()
$this->assertEquals([], $roles[3]->countries2->pluck('id')->all());
}

public function testEagerLoadingWithHasOneDeep()
{
$roles = Role::with('country')->get();

$this->assertEquals(71, $roles[0]->country->id);
$this->assertEquals(71, $roles[1]->country->id);
$this->assertNull($roles[3]->country);
}

public function testLazyEagerLoading()
{
$roles = Role::all()->load('countries');
Expand Down
6 changes: 6 additions & 0 deletions tests/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Relations\HasMany;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Staudenmeir\EloquentJsonRelations\JsonKey;
use Staudenmeir\EloquentJsonRelations\Relations\HasManyJson;
Expand All @@ -22,6 +23,11 @@ public function countries2(): HasManyDeep
return $this->hasManyDeepFromRelations($this->usersWithObjects(), (new User())->country());
}

public function country(): HasOneDeep
{
return $this->hasOneDeepFromRelations($this->users(), (new User())->country());
}

public function permissions(): HasMany
{
return $this->hasMany(Permission::class);
Expand Down
6 changes: 6 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;
use Staudenmeir\EloquentHasManyDeep\HasOneDeep;
use Staudenmeir\EloquentHasManyDeep\HasRelationships;
use Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson;

Expand All @@ -29,6 +30,11 @@ public function locale(): BelongsTo
return $this->belongsTo(Locale::class, 'options->locale_id');
}

public function permission(): HasOneDeep
{
return $this->hasOneDeepFromRelations($this->roles(), (new Role())->permissions());
}

public function permissions(): HasManyDeep
{
return $this->hasManyDeepFromRelations($this->roles(), (new Role())->permissions());
Expand Down

0 comments on commit 88919ad

Please sign in to comment.