Skip to content

修正动态API功能检索所有模型的时候检索到抽象类型#144

Merged
slowlyo merged 1 commit intoslowlyo:masterfrom
asundust:master
Aug 6, 2024
Merged

修正动态API功能检索所有模型的时候检索到抽象类型#144
slowlyo merged 1 commit intoslowlyo:masterfrom
asundust:master

Conversation

@asundust
Copy link
Contributor

@asundust asundust commented Aug 6, 2024

现象

  • 冲突发现于laravel-lang/common的扩展包,在使用动态API的功能时候加载所有Model的时候报错,报错提示如下。
Illuminate\Contracts\Container\BindingResolutionException
Target [LaravelLang\Models\Eloquent\Translation] is not instantiable.

定位

  • 打开该文件vendor/laravel-lang/models/src/Eloquent/Translation.php发现该类使用了抽象类声明。
  • 在3.9.5版本中的该行vendor/slowlyo/owl-admin/src/Services/AdminRelationshipService.php:89实例过程中发生的报错。
$models = collect($classMap)
      ->keys()
      ->filter(fn($item) => str_contains($item, 'Models\\'))
      ->filter(fn($item) => @class_exists($item))
      ->filter(fn($item) => (new \ReflectionClass($item))->isSubclassOf(Model::class))
      ->merge($modelDirClass)
      ->unique()
      ->filter(fn($item) => in_array(app($item)->getTable(), $tables)) // 错误行
      ->values()
      ->map(fn($item) => [
          'label' => Str::of($item)->explode('\\')->pop(),
          'table' => app($item)->getTable(),
          'value' => $item,
      ]);

解决

  • 筛选中加入去除抽象类的检查。
$models = collect($classMap)
            ->keys()
            ->filter(fn($item) => str_contains($item, 'Models\\'))
            ->filter(fn($item) => @class_exists($item))
            ->filter(fn($item) => (new \ReflectionClass($item))->isSubclassOf(Model::class) && !(new \ReflectionClass($item))->isAbstract()) // 修改行
            ->merge($modelDirClass)
            ->unique()
            ->filter(fn($item) => in_array(app($item)->getTable(), $tables))
            ->values()
            ->map(fn($item) => [
                'label' => Str::of($item)->explode('\\')->pop(),
                'table' => app($item)->getTable(),
                'value' => $item,
            ]);

@slowlyo slowlyo merged commit 29d7f7b into slowlyo:master Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants