mirror of
https://github.com/barryvdh/laravel-ide-helper.git
synced 2026-08-18 01:57:13 +00:00
Prevent undefined property errors (#877)
If the property name matches the foreign key name creating a BelongsTo relation triggers an undefined property error. Disabling constraints prevents accessing the property and thus prevents the error.
This commit is contained in:
@@ -230,7 +230,9 @@ class ModelsCommand extends Command
|
||||
$ignore[] = $name;
|
||||
$this->nullableColumns = [];
|
||||
} catch (\Throwable $e) {
|
||||
$this->error("Exception: " . $e->getMessage() . "\nCould not analyze class $name.\n\nTrace:\n".$e->getTraceAsString());
|
||||
$this->error("Exception: " . $e->getMessage() .
|
||||
"\nCould not analyze class $name.\n\nTrace:\n" .
|
||||
$e->getTraceAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -476,7 +478,7 @@ class ModelsCommand extends Command
|
||||
// php 7.x type or fallback to docblock
|
||||
$type = (string)$this->getReturnTypeFromDocBlock($reflection);
|
||||
}
|
||||
|
||||
|
||||
$file = new \SplFileObject($reflection->getFileName());
|
||||
$file->seek($reflection->getStartLine() - 1);
|
||||
|
||||
@@ -510,7 +512,12 @@ class ModelsCommand extends Command
|
||||
continue;
|
||||
}
|
||||
|
||||
$relationObj = $model->$method();
|
||||
// Adding constraints requires reading model properties which
|
||||
// can cause errors. Since we don't need constraints we can
|
||||
// disable them when we fetch the relation to avoid errors.
|
||||
$relationObj = Relation::noConstraints(function () use ($model, $method) {
|
||||
return $model->$method();
|
||||
});
|
||||
|
||||
if ($relationObj instanceof Relation) {
|
||||
$relatedModel = '\\' . get_class($relationObj->getRelated());
|
||||
|
||||
@@ -62,4 +62,9 @@ class Simple extends Model
|
||||
{
|
||||
return $this->belongsTo(AnotherModel::class);
|
||||
}
|
||||
|
||||
public function relationBelongsToSameNameAsColumn(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(AnotherModel::class, __FUNCTION__);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@ use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\ModelsOtherNamespace\AnotherModel $relationBelongsToInAnotherNamespace
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationBelongsToMany
|
||||
* @property-read int|null $relation_belongs_to_many_count
|
||||
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\ModelsOtherNamespace\AnotherModel $relationBelongsToSameNameAsColumn
|
||||
* @property-read \Illuminate\Database\Eloquent\Collection|\Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple[] $relationHasMany
|
||||
* @property-read int|null $relation_has_many_count
|
||||
* @property-read \Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Relations\Models\Simple $relationHasOne
|
||||
@@ -136,6 +137,11 @@ class Simple extends Model
|
||||
{
|
||||
return $this->belongsTo(AnotherModel::class);
|
||||
}
|
||||
|
||||
public function relationBelongsToSameNameAsColumn(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(AnotherModel::class, __FUNCTION__);
|
||||
}
|
||||
}
|
||||
|
||||
PHP;
|
||||
|
||||
Reference in New Issue
Block a user