Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HasOneThrough / HasManyThrough relationships #172

Merged
merged 12 commits into from
Apr 12, 2024
11 changes: 0 additions & 11 deletions .github/workflows/code-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ jobs:
extensions: ${{ env.extensions }}
coverage: none

- name: Setup dependency cache
id: composercache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: composer install --no-interaction --no-progress --no-scripts

Expand Down
11 changes: 0 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,6 @@ jobs:
extensions: ${{ env.extensions }}
coverage: none

- name: Setup dependency cache
id: composercache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composercache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer dependencies
run: composer install --no-interaction --no-progress --no-scripts

Expand Down
31 changes: 18 additions & 13 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/AttachMany.php

-
Expand Down Expand Up @@ -177,7 +177,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/AttachOne.php

-
Expand Down Expand Up @@ -222,7 +222,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/BelongsTo.php

-
Expand Down Expand Up @@ -272,7 +272,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Model\\:\\:getRelationDefinition\\(\\)\\.$#"
count: 3
count: 2
path: src/Database/Relations/BelongsToMany.php

-
Expand All @@ -292,7 +292,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/BelongsToMany.php

-
Expand Down Expand Up @@ -357,7 +357,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/HasMany.php

-
Expand Down Expand Up @@ -402,7 +402,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/HasManyThrough.php

-
Expand All @@ -425,14 +425,19 @@ parameters:
count: 1
path: src/Database/Relations/HasManyThrough.php

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getQualifiedFirstKeyName\\(\\)\\.$#"
count: 12
path: src/Database/Relations/Concerns/DefinedConstraints.php

-
message: "#^Access to an undefined property Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\BelongsToMany\\:\\:\\$countMode\\.$#"
count: 1
path: src/Database/Relations/HasOne.php

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/HasOne.php

-
Expand Down Expand Up @@ -467,7 +472,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/HasOneThrough.php

-
Expand Down Expand Up @@ -497,7 +502,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/MorphMany.php

-
Expand Down Expand Up @@ -557,7 +562,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/MorphOne.php

-
Expand Down Expand Up @@ -612,7 +617,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/MorphTo.php

-
Expand Down Expand Up @@ -642,7 +647,7 @@ parameters:

-
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Relations\\\\Relation\\:\\:getForeignKey\\(\\)\\.$#"
count: 3
count: 1
path: src/Database/Relations/MorphToMany.php

-
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ parameters:
# Exclude PHP Parser files
- src/Parse/PHP/ArrayFile.php
- src/Parse/PHP/ArrayPrinter.php
- src/Foundation/Console/KeyGenerateCommand.php
disableSchemaScan: true
databaseMigrationsPath:
- src/Auth/Migrations
- src/Database/Migrations
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ protected function handleRelation($relationName)
case 'hasOneThrough':
case 'hasManyThrough':
$relation = $this->validateRelationArgs($relationName, ['key', 'throughKey', 'otherKey', 'secondOtherKey'], ['through']);
$relationObj = $this->$relationType($relation[0], $relation['through'], $relation['key'], $relation['throughKey'], $relation['otherKey'], $relation['secondOtherKey']);
$relationObj = $this->$relationType($relation[0], $relation['through'], $relation['key'], $relation['throughKey'], $relation['otherKey'], $relation['secondOtherKey'], $relationName);
break;

default:
Expand Down
21 changes: 15 additions & 6 deletions src/Database/Relations/Concerns/DefinedConstraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ trait DefinedConstraints
*/
public function addDefinedConstraints()
{
$args = $this->parent->getRelationDefinition($this->relationName);
if (isset($this->farParent)) {
// hasOneThrough / hasManyThrough relations
$parent = $this->farParent;
} else {
$parent = $this->parent;
}
$args = $parent->getRelationDefinition($this->relationName);

$this->addDefinedConstraintsToRelation($this, $args);

Expand Down Expand Up @@ -62,13 +68,16 @@ public function addDefinedConstraintsToRelation($relation, ?array $args = null)
if ($relation instanceof BelongsToManyBase) {
$relation->countMode = true;
}

if (isset($relation->farParent)) {
$foreignKey = $relation->getQualifiedFirstKeyName();
} else {
$foreignKey = $relation->getForeignKey();
}
$countSql = $this->parent->getConnection()->raw('count(*) as count');

$relation
->select($relation->getForeignKey(), $countSql)
->groupBy($relation->getForeignKey())
->orderBy($relation->getForeignKey())
->select($foreignKey, $countSql)
->groupBy($foreignKey)
->orderBy($foreignKey)
;
}
}
Expand Down